我正在尝试执行以下操作:
- 只有选中所有复选框,用户才能继续下一步
- 在其他情况下,如果用户点击按钮,它应该弹出警报消息
如果复选框没有全部选中,我无法弄清楚第二部分。这是我的代码:
$("input[type='checkbox'].checkbox").change(function(){
var a = $("input[type='checkbox'].checkbox");
if (a.length == a.filter(":checked").length){
$('.button').on('click',function(){
// do something, no issue here
});
} else {
// do something else, can't figure this out
}
});
最佳答案
HTML:
<input type='checkbox' id="1" />
<br />
<input type='checkbox' id="2" />
<br />
<button id="btn">Click</button>
JQuery:
$("#btn").click(function() {
// GET ALL TOTAL CHECKED CHECKBOXES
var total_checked = $('input[type="checkbox"]:checked').length;
// TOTAL CHECK BOXES
var total_boxes = $('input[type="checkbox"]').length
if(total_checked === total_boxes) {
alert('Checked All');
} else {
alert('Please select all check boxes'); return false;
}
});
fiddle :http://jsfiddle.net/A9MBS/1/
建议:用户点击 1 个特定复选框后,动态选择全部
$('input[type="checkbox"]').click(function() {
if($(this).is(':checked')) {
$('input[type="checkbox"]').prop('checked', true);
} else {
$('input[type="checkbox"]').prop('checked', false);
}
});
fiddle :http://jsfiddle.net/A9MBS/2/
关于javascript - 如果所有复选框都被选中,则执行此操作,否则执行此操作(jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20431838/