我在应用程序中使用复选框,但遇到了一个问题,尽管我付出了努力,但仍无法解决。
function allChecked(status,set){
alert(set);
if(status)
$('input[type=checkbox]').attr('checked','true');
else
$('input[type=checkbox]').attr('checked','');
}
上述函数根据“status”的值选择或取消选择所有复选框。 “set”是一个数字,指定应选择多少个复选框。所以我希望代码根据“set”的值选择复选框。我该怎么做?
最佳答案
假设您想要选中或取消选中前 N 个复选框(按照它们在 DOM 中出现的顺序),您可以使用 slice实现此目的的方法:
function allChecked(status, set) {
var checkboxes = $('input[type=checkbox]').slice(0, set);
if (status) {
checkboxes.attr('checked', 'checked');
}
else {
checkboxes.removeAttr('checked');
}
}
关于jquery - 使用 Jquery 在 jsp 页面中选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027860/