简体   繁体   English

如何在jQuery中对单选按钮和复选框的值求和

[英]How to sum value of radio button and checkbox in jquery

I have a code already working which calculates the sum of the radio buttons. 我有一个已经在工作的代码,可以计算单选按钮的总和。 I changed my mind in one of the radio button group and decided to make it a checkbox. 我在一个单选按钮组中改变了主意,决定将其设为复选框。 When I tick items on the checkbox, the sum returns NaN . 当我在复选框上打勾时,总和返回NaN What would I add/change in my jquery in order for it to recognize the checkbox value? 我将在jquery中添加/更改什么以便使其能够识别复选框值?

Here's my code: 这是我的代码:

JQuery JQuery的

  < script type = "text/javascript" > function calcscore() { $(".calc:checked").each(function() { score += parseInt($(this).val(), 10); }); $('#price').text(score.toFixed(2)); $("input[name=sum]").val(score) } $().ready(function() { $(".calc").change(function() { calcscore() }); }); < /script> 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <li> <label> <input class="calc" type="radio" name="rad1" id="rad1" /> </label> <input type="hidden" name="rad1" value="100"> </li> <li> <label> <input class="calc" type="checkbox" name="check1" id="check1" value="200" /> </label> <input type="hidden" name="check1" value="200"> </li> <p>Total: PHP <span id="price">0</span> </p> 

Appreciate all the help. 感谢所有帮助。

This code code should answer your question: 此代码应回答您的问题:

 function calcscore() { score = 0; $(".calc:checked").each(function () { score += Number($(this).val()); }); $("#price").text(score.toFixed(2)); $("#sum").val(score) } $().ready(function () { $(".calc").change(function () { calcscore() }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <li> <label> <input class="calc" type="radio" name="rad1" id="rad1" value="100" /> </label> </li> <li> <label> <input class="calc" type="checkbox" name="check1" id="check1" value="200" /> </label> </li> <input type="hidden" name="sum" id="sum" value="0"> <p>Total: PHP <span id="price">0</span> </p> 

for your markup and usecase as it stands now in the question.try this 用于您现在在问题中的标记和用例

 $('.calc').on('click', function() { var sum = 0; $('.calc:checked').each(function() { sum += Number($(this).parent().next().val()) }) $('#price').text(sum) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <li> <label> <input class="calc" type="radio" name="rad1" id="rad1" /> </label> <input type="hidden" name="rad1" value="100"> </li> <li> <label> <input class="calc" type="checkbox" name="check1" id="check1" value="200" /> </label> <input type="hidden" name="check1" value="200"> </li> <p>Total: PHP <span id="price">0</span> </p> 

try this,you dont need hidden fields for values.i removed them and used value property of checkbox. 试试这个,您不需要值的隐藏字段。我删除了它们并使用了复选框的value属性。

 $('.calc').on('click', function() { var sum = 0; $('.calc:checked').each(function() { sum += Number($(this).val()) }) $('#price').text(sum) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <li> <label> <input class="calc" type="checkbox" name="check1" value="100" id="rad1" /> </label> </li> <li> <label> <input class="calc" type="checkbox" name="check1" value="200" id="check1" value="200" /> </label> </li> <p>Total: PHP <span id="price">0</span> </p> 

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

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