jQuery +累加复选框值 [英] Jquery + Add Up Checkbox Values

查看:58
本文介绍了jQuery +累加复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有很多复选框.每个复选框用于一个不同的项目(虚拟玫瑰),并且玫瑰具有不同的值.我已将玫瑰的值放入复选框的value ="中.我需要将已勾选的复选框的总价值相加-例如:100、230等.唯一的其他因素是我只需要添加带有"giftsRosesCheckBox"类的复选框.

I have a page with a number of checkboxes. Each Checkbox is for a different item (virtual Rose) and rose has a different value. I have put the value of the rose into the value="" of the checkbox. I need to add up the total value of the ticked checkboxes - eg: 100, 230 etc. The only other factor is I need to only add up checkboxes with the class "giftsRosesCheckBox".

<table id="tableGiftsWhiteRose"><tr>
  <td colspan="2">White Rose</td></tr><tr>
  <td><input type="checkbox" class="giftsRosesCheckBox" value="100"/></td>
  <td class="credits">100 credits</td> 
</tr></table>

<table  id="tableGiftsRedRose"><tr>
  <td colspan="2">Red Rose</td></tr><tr>
  <td><input type="checkbox" class="giftsRosesCheckBox" value="130"/></td>
  <td class="credits">130 credits</td>      
</tr></table>

<table><tr>
  <td colspan="2">Stunning Red Rose</td></tr><tr>
  <td><input type="checkbox" class="giftsRosesCheckBox" value="150"/></td>
  <td class="credits">150 credits</td>      
</tr></table>

我真的不确定如何执行此操作,因此朝正确方向的推动会很棒.

I'm really not sure how to do this so a push in the right direction would be great.

非常感谢.

推荐答案

$(".giftsRosesCheckBox").click(function() {
    var total = 0;
    $(".giftsRosesCheckBox:checked").each(function() {
        total += parseInt($(this).val(), 10);
    });
    alert(total);
});

这篇关于jQuery +累加复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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