Javascript JQuery更改事件循环
我有几个复选框。如果用户检查了其中任何一个,我必须查看是否允许他们这样做,如果不允许,通知他们并取消选中。然而,这让我陷入了一个循环,因为它再次触发了更改!我怎么能过来Javascript JQuery更改事件循环,javascript,jquery,forms,loops,event-binding,Javascript,Jquery,Forms,Loops,Event Binding,我有几个复选框。如果用户检查了其中任何一个,我必须查看是否允许他们这样做,如果不允许,通知他们并取消选中。然而,这让我陷入了一个循环,因为它再次触发了更改!我怎么能过来 $('#step_1, #step_2, #step_3, #step_4').change(function(event) { if(!$('#section_2_active').attr('checked')){ alert('You can not select output op
$('#step_1, #step_2, #step_3, #step_4').change(function(event) {
if(!$('#section_2_active').attr('checked')){
alert('You can not select output options when "Create Output" has not been selected.');
$(this).attr('checked', false);
$('#other_stages :checkbox:not(#section_2_active, #co_t)').change();
}
});
我不能先解除绑定,然后再重新绑定更改事件,因为还有另一个插件依赖于此事件
我还有其他选择吗?我可以为每个复选框附加一个函数调用,但我希望有更优雅的东西,如果没有,我将默认为这个
感谢所有人的帮助最简单的更改如下:
var inClickHandler = false;
function OnChecked(evt) {
if (inClickHandler) {
inClickHandler = false;
return;
}
inClickHandler = true;
// Do your stuff.
}
$('#step_1, #step_2, #step_3, #step_4').change(function(event, loop) {
if(!$('#section_2_active').attr('checked')){
alert('You can not select output options when "Create Output" has not been selected.');
$(this).attr('checked', false);
if(loop !== false)
$('#other_stages :checkbox:not(#section_2_active, #co_t)').trigger('change', false);
}
});
您可以使用将附加参数传递给处理程序,我们将使用循环
。如果为false
,则不要再次运行其他元素触发器,如下所示:
var inClickHandler = false;
function OnChecked(evt) {
if (inClickHandler) {
inClickHandler = false;
return;
}
inClickHandler = true;
// Do your stuff.
}
$('#step_1, #step_2, #step_3, #step_4').change(function(event, loop) {
if(!$('#section_2_active').attr('checked')){
alert('You can not select output options when "Create Output" has not been selected.');
$(this).attr('checked', false);
if(loop !== false)
$('#other_stages :checkbox:not(#section_2_active, #co_t)').trigger('change', false);
}
});
你为什么打这个电话<代码>$(“#其他#阶段:复选框:不(#第#2#u段处于活动状态,#co#t)”)。更改()代码>据我所知,你不需要这个,它也不会循环。除非使用
event.preventDefault(),否则该复选框仍将选中
和.attr('checked',false)代码>不会触发更改,可能是因为您遇到的问题