这是我的表单,其中有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>
现在我如何将相应的事件绑定到相应的复选框(例如,如果用户单击加法,则仅执行加法),如果用户一次选择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的初学者,如何执行此代码答案中建议的复选框事件?
答案 0 :(得分:2)
在您的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();
}
}