Javascript ';复选框更改';无限循环的结果
我的要求是选中/取消选中复选框更改事件上的所有其他复选框 但结果是无限循环Javascript ';复选框更改';无限循环的结果,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我的要求是选中/取消选中复选框更改事件上的所有其他复选框 但结果是无限循环 $(“.chk”).change(函数(事件){ var$this=$(this); var id=$this.attr('id'); var chkothercheckbox='on'; 如果($this.is(':checked')){ chkOtherCheckboxes='关闭'; } $('input:checkbox:not(“#”+id+”)).bootstraptogle(chkothercheckbox
$(“.chk”).change(函数(事件){
var$this=$(this);
var id=$this.attr('id');
var chkothercheckbox='on';
如果($this.is(':checked')){
chkOtherCheckboxes='关闭';
}
$('input:checkbox:not(“#”+id+”)).bootstraptogle(chkothercheckbox);
});
禁用引导切换
并更改输入,然后再次启用
$(".chk").change(function(event) {
var id = this.id;
var status = !this.checked;
$('input:checkbox:not("#' + id + '")').each(function(){
$(this).bootstrapToggle('destroy');
$(this).prop('checked', status);
$(this).bootstrapToggle();
});
});
当用户单击复选框时,您将再次更改相同的复选框
.chk
User click checkbox --> triggers change function -->
--> changes status of checkboxes( including itself) -->
--> again triggers change function
//无限循环
确保不会触发对当前复选框的更改
User click checkbox --> triggers change function -->
--> changes status of checkboxes( including itself) -->
--> again triggers change function
让我知道它是否有用。因为它在再次调用
change
函数的函数中被切换。它更像是递归。@NikhilWagh它不是递归,因为递归必须有一个停止条件。请分享您所有相关的codeprop('checked')在这里不起作用。Bootstraptogle是我唯一可以使用开关的东西。@Devi当您尝试使用bootstraptogle('disable')
并启用时,情况如何?使用bootstraptogle('disable')
将导致选中复选框disable@devi,很抱歉,我的本意是销毁而不是禁用。谢谢你的建议。但我的要求是-选中任何复选框都会导致取消选中所有其他复选框。我收到了你的要求。我只是让您知道,您正在再次触发同一复选框上的事件,这不是正确的方式:)。上述答案可能会解决您的问题,但不是解决此问题的最佳方法。