3

我有一个包含 16 个复选框的表单。我正在尝试跟踪选中的框的数量并在表单下方提供即时反馈,但我无法获取 numSelected 变量来更新更改。

这是我的脚本:

$(document).ready(function () {
// the jQuery.iCheck library wraps the input in a div for styling   
$('input').iCheck({
    checkboxClass: 'icheckbox_square-red'
});

// count the checkboxes
var cb = $(":checkbox"),
    numSelected = 0,
    numLeft = 2 - parseInt(numSelected, 10);
$(cb).change(function () {
    //alert("a checkbox was checked!");
    var $this = $(this);         
    var numSelected = $this(':checked').length;
    $('#status-box').html("Out of "+$this.length+" images you have selected "+numSelected+" for processing, you have "+numLeft+" remaining");
});
});

这是我放在一起的jsfiddle ,感谢您的帮助!

4

2 回答 2

4

检查插件提供的回调 API。所以基本上使用ifToggled回调:

var $cb = $('input:checkbox');
$cb.iCheck({
    checkboxClass: 'icheckbox_square-red'
}).on('ifToggled', function() {
    var numSelected = $cb.filter(':checked').length,
        numLeft = 2 - numSelected;
    $('#status-box').html("Out of "+$cb.length+" images you have selected "+numSelected+" for processing, you have "+numLeft+" remaining");
});

小提琴

不确定负的剩余数字是否是有意的,但这似乎离题了,我将把业务逻辑留给你。=]

于 2013-07-05T19:44:15.723 回答
0

尝试这个:

$('.image-checkbox').change(function() {
    var numSelected = $("input:checkbox:checked").length;
    alert(numSelected);
});
于 2013-07-05T19:45:15.290 回答