简体   繁体   English

复选框点击事件

[英]Checkbox click event

I have a group of checkboxes inside a table where I want to change the value of a different column in the row when the checkbox is checked. 我在表中有一组复选框,当选中该复选框时,我想在其中更改行中另一列的值。 I have researched for a solution but nothing I have found seems to solve my problem. 我已经研究过一种解决方案,但是没有发现似乎可以解决我的问题。 I realize that my stumbling block is my unfamiliarity with jquery so any suggestions would help. 我意识到我的绊脚石是我对jquery的不熟悉,因此任何建议都将有所帮助。 Ultimately I wish to total the columns where the change has occurred to get a total. 最终,我希望对发生更改的列进行总计,以得出总计。 So if an answer included ideas about that as well I would not complain. 因此,如果答案中也包含关于此的想法,我不会抱怨。 Thanks as always, you are a great group. 一如既往的感谢,您是一个很棒的团体。

  HTML

    <tr>
        <td><input name="amputeeGolfer" type="checkbox" id="amputeeGolfer" value="amputee" onchange="changeFee"/>
            <label for="amputeeGolfer">Amputee Golfer</label></td>
        <td align="left"><label for="amputeeFee">$50.00</label></td>
        <td></td>
        <td><input name="amputeeFee" type="number" id="amputeeFee" class="tblRight" size="10" value="0.00"/></td>
   </tr>

jquery

   <script>
   function changeFee(val) {
     $('#amputeeFee').val(), "$50.00";
        }
   </script>

You can get closest tr closest('tr') to assure input in same row with check box and find input with name find("input[name='amputeeFee']") and change value for it. 您可以获取最接近的tr closest('tr')以确保输入与复选框在同一行中,并找到名称为find("input[name='amputeeFee']")并更改其值。

function changeFee(val) {
   var amputeeFee = $(val).closest('tr').find("input[name='amputeeFee']");

   if($(val).prop("checked")){
       amputeeFee.val(50.00);
   }
   else{
      amputeeFee.val(0);
   }
}

 function changeFee(val) { var amputeeFee = $(val).closest('tr').find("input[name='amputeeFee']"); //console.log(amp.length); if($(val).prop("checked")){ amputeeFee.val(50.00); } else{ amputeeFee.val(0); } } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td><input name="amputeeGolfer" type="checkbox" id="amputeeGolfer" value="amputee" onchange="changeFee(this)"/> <label for="amputeeGolfer">Amputee Golfer</label></td> <td align="left"><label for="amputeeFee">$50.00</label></td> <td></td> <td><input name="amputeeFee" type="number" id="amputeeFee" class="tblRight" size="10" value="0.00"/></td> </tr> </table> 

Fully functioning snippet. 功能齐全的代码段。 No jQuery required! 无需jQuery!

When the onchange event fires, it checks whether the checkbox was just checked or unchecked, and toggles the price accordingly. onchange事件触发时,它会检查复选框是否已选中或未选中,并相应地切换价格。 It can even be combined with all sorts of other checkboxes. 它甚至可以与其他各种复选框结合使用。

 function togglePrice(element,price){ if(element.checked){ document.getElementById("amputeeFee").value = parseInt(document.getElementById("amputeeFee").value) + price; }else{ document.getElementById("amputeeFee").value = parseInt(document.getElementById("amputeeFee").value) - price; } } 
 <tr> <td><input name="amputeeGolfer" type="checkbox" id="amputeeGolfer" value="amputee" onchange="togglePrice(this,50);"/> <label for="amputeeGolfer">Amputee Golfer</label></td> <td align="left"><label for="amputeeFee">$50.00</label></td> <td></td> <td><input name="amputeeFee" type="number" id="amputeeFee" class="tblRight" size="10" value="0"/></td> </tr> 

It works perfectly and you can even set how much the checkbox adds to the cost! 它运行完美,您甚至可以设置复选框增加的成本!

To call a JavaScript function like changeFee(val) in an HTML's element event, the funciton has to be called as the same in the script. 要在HTML的element事件中调用诸如changeFee(val)类的JavaScript函数,必须在脚本中调用该函数。 As all functions in an HTML' event: <element onclick="myFunction()"> , and not <element onclick="myFunction"> beacuse it doesn't reconize it's a function in JavaScript. 由于HTML事件中的所有函数: <element onclick="myFunction()"> ,而不是<element onclick="myFunction">因为它不会重新协调它是JavaScript中的函数。

Then the code will be: 然后代码将是:

<tr>
    <td><input name="amputeeGolfer" type="checkbox" id="amputeeGolfer" value="amputee" onchange="changeFee(this.value)"/>
        <label for="amputeeGolfer">Amputee Golfer</label></td>
    <td align="left"><label for="amputeeFee">$50.00</label></td>
    <td></td>
    <td><input name="amputeeFee" type="number" id="amputeeFee" class="tblRight" size="10" value="0.00"/></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM