如何将一个或多个算术运算的事件绑定到复选框? [英] how can I bind event of one or more arithmatic operation to checkbox?

查看:15
本文介绍了如何将一个或多个算术运算的事件绑定到复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

here is my form in which there are 4 checkboxes for 4 operations.

<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 个复选框,假设添加和减去,则只有这两个操作才会有去表演 ?

now how can i bind the respective event to respective checkbox (ex.If user click on addition then then only addition will perform) and in case user select 2 checkboxes at a time suppose add and subtract then only those two operation will have to perform ?

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

here is my php code where I am doing the rest code

 <?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 初学者,如何执行此代码的答案中建议的复选框事件?

I am beginner in php ,how can I do the checkbox event that suggested in the answer for this code ?

推荐答案

在你的 html 中,添加一个 onclick 事件如下:

In your html, add a onclick event as below:

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

在您的 js 中,实现 doAddOperation() 如下.为所有操作执行此操作.

In your js, implement the doAddOperation() as below. Do this for all operation.

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

这篇关于如何将一个或多个算术运算的事件绑定到复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆