在下面的示例中,如果我单击一个按钮来重置我所有的复选框。我的下一个按钮只选择某些按钮将不起作用,但当我第一次单击它时它会起作用。有什么想法吗?
$(function(){
jQuery('.selectA-button').click(function(){
$('.circle1L').addClass('circle-on');
$('.circle2L').addClass('circle-on');
$('.circle3L').addClass('circle-on');
$('.circle4L').addClass('circle-on');
$('.circle5L').addClass('circle-on');
$('.circle6L').addClass('circle-on');
$('input[name=onoffswitch1L]').attr('checked',true);
$('input[name=onoffswitch2L]').attr('checked',true);
$('input[name=onoffswitch3L]').attr('checked',true);
$('input[name=onoffswitch4L]').attr('checked',true);
$('input[name=onoffswitch5L]').attr('checked',true);
$('input[name=onoffswitch6L]').attr('checked',true);
});
$(function(){
jQuery('.reset-button').click(function(){
jQuery('.box-on').toggleClass('box-on');
jQuery('.circle-on').toggleClass('circle-on');
jQuery('.circle-on2').toggleClass('circle-on2');
$('input:checkbox').attr('checked',false);
});
});
请参阅此行:
$('input:checkbox').attr('checked',false);
改用.removeAttr
https://api.jquery.com/removeAttr/
checked="false"
仍将包含选中的属性
https://www.w3schools.com/tags/att_input_checked.asp
用这个测试:
<input type="checkbox" checked="false"/>
上面的标记显示了一个选中的复选框。
===
============================================================================编辑:实际上最好使用prop('checked', true);
和.prop('checked', false);
见小提琴:https://jsfiddle.net/dwrr3z35/6/