gpt4 book ai didi

javascript - 如何将一个或多个算术运算的事件绑定(bind)到复选框?

转载 作者:行者123 更新时间:2023-11-30 14:17:36 26 4
gpt4 key购买 nike

这是我的表单,其中有 4 个复选框用于 4 个操作。

<form action="" method="POST">
Select no.of questions:<input type="number" name="que" value="que">
<br> <br>
Select no. of series: <select name="select_box">
<option value="0">Please Select</option>
<option value="1"> 2 </option>
<option value="2"> 3 </option>
</select>

<br><br>
Select number type(in digits) <input type="number" name="digits"
value="digits">
<br><br>
Select operations:<br />
<input type="checkbox" id="add" name="operation" value="addition"
id="add"><label>Addition</label>
<br/>
<input type="checkbox" id="sub" name="operation" value="substraction"
id="sub"><label>substraction</label><br/>
<input type="checkbox" id="add" name="operation" value="multiplication"
id="mul"><label>Multiplication</label><br/>
<input type="checkbox" id="add" name="operation" value="division"
id="div"><label>Division</label><br/><br / >

<br><br>
<input type="submit" name="submit" value="Generate"><br>
<br>
</form>

现在我如何将相应的事件绑定(bind)到相应的复选框(例如,如果用户单击加法,则只执行加法),如果用户一次选择 2 个复选框,假设加法和减法,那么只有这两个操作会有去表演 ?

这是我的 php 代码,我正在执行其余代码

 <?php

//for 1 digits
if($_POST['digits'] == 1)
{
$que = $_POST['que'];
for ($x = 1; $x <=$que; $x++)
{

$rand1 = rand(0,9);
$rand2 = rand(0,9);
$rand3 = rand(0,9);

$operator = array('+', '-','*','/');
$randoperator = $operator[rand(0,3)];
switch ($randoperator) {
case "+":
$finalvalue = $rand1 + $rand2 + $rand3;
break;
case "-":
$finalvalue = $rand1 - $rand2 - $rand3;
break;
case "*":
$finalvalue = $rand1 * $rand2 * $rand3;
break;
case "/":
$finalvalue = $rand1 / $rand2 / $rand3;
break;

}
echo ("This is Q(".$x."):"), $rand1 . $randoperator . $rand2 .
$randoperator . $rand3 . '=' . $finalvalue ,'<br /><br />';
}
}
elseif(isset($_POST['digits']) == 2)
{
$qu = $_POST['que'];
for ($y = 1; $y <=$qu; $y++)
{
$rand1 = rand(10, 99);
$rand2 = rand(10, 99);
$rand3 = rand(10, 99);

$operator2 = array('+', '-','*','/');
$randoperator2 = $operator2[rand(0, 3)];
switch ($randoperator2) {
case "+":
$finalvalue2 = $rand1 + $rand2 + $rand3;
break;
case "-":
$finalvalue2 = $rand1 - $rand2 - $rand3;
break;
case "*":
$finalvalue2 = $rand1 * $rand2 * $rand3;
break;
case "/":
$finalvalue2 = $rand1 / $rand2 / $rand3;
break;

}
echo ("This is Q(".$y."):"), $rand1 . $randoperator2 . $rand2 .
$randoperator2 . $rand3 . '=' . $finalvalue2 ,'<br /><br />';
}
}
elseif(isset($_POST['digits']) == 3)
{
$q = $_POST['que'];
for ($p = 1; $p <=$q; $p++)
{


$rand1 = rand(100,999);
$rand2 = rand(100,999);
$rand3 = rand(100,999);

$operator3 = array('+', '-','*','/');
$randoperator3 = $operator3[rand(0,3)];
switch ($randoperator3) {
case "+":
$finalvalue3 = $rand1 + $rand2 +$rand3;
break;
case "-":
$finalvalue3 = $rand1 - $rand2 - $rand3;
break;
case "*":
$finalvalue3 = $rand1 * $rand2 * $rand3;
break;
case "/":
$finalvalue3 = $rand1 / $rand2 / $rand3;
break;

}
echo ("This is Q(".$p."):"), $rand1 . $operator3 . $rand2 . $operator3 .
$rand3 . '=' . $finalvalue3 ,'<br /><br />';
}
}
else{
//invalid;
}
?>

我是 php 的初学者,我该如何执行此代码的答案中建议的复选框事件?

最佳答案

在您的 html 中,添加一个 onclick 事件,如下所示:

<input type="checkbox" id="add" name="operation" value="addition" id="add" onclick="doAddOperation()"><label>Addition</label>

在您的 js 中,按如下方式实现 doAddOperation()。对所有操作执行此操作。

function doAddOperation(){
var checkbox = document.getElementById('add');
if (checkbox.checked == true){
add();
}
}

关于javascript - 如何将一个或多个算术运算的事件绑定(bind)到复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53294631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com