我有一个包含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,感谢任何帮助!
答案 0 :(得分:4)
检查提供的插件callbacks 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");
});
不确定是否存在负数剩余数字,但由于这似乎偏离主题,我会将业务逻辑留给您。 =]
答案 1 :(得分:0)
试试这个:
$('.image-checkbox').change(function() {
var numSelected = $("input:checkbox:checked").length;
alert(numSelected);
});