简体   繁体   English

如何使用JQuery实时添加复选框的值?

[英]How to add up values of checkboxes in real time using JQuery?

Should be easy, but this code isn't working for me: 应该很简单,但是这段代码对我不起作用:

$("#checkboxdiv input:checkbox").change(function() {
            var total = 0;
            $('#checkboxdiv input:checkbox:checked').each(function() {
                total += parseInt(this.value, 10);
            });
            $('#totalsdiv').text(total);
        });

The checkboxes to consider are all in the #checkbox div, and I'm trying to fill in the div with the total amount. 要考虑的复选框全部在#checkbox div中,我正在尝试在div中填充总数。

Thanks. 谢谢。

As the code is fine and works , it's probable there is an unrelated error elsewhere. 由于代码可以正常工作 ,因此很可能在其他地方存在不相关的错误。

You should use the developer tools to debug and hunt it. 您应该使用开发人员工具进行调试和搜寻。

您可以尝试侦听CheckBox上的click事件,而不是change事件。

Without knowing your problem I'll guess at this 不知道你的问题,我会猜这

$("#checkboxdiv input:checkbox").change(function() {
            var total = 0;
            $('#checkboxdiv input:checkbox:checked').each(function() {
                total += parseInt($(this).val(), 10);
            });
            $('#totalsdiv').text(total);
        });

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

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