jQuery变更事件循环
-
09-10-2019 - |
题
我有几个复选框。如果用户检查其中的任何一个,我必须查看是否允许他们这样做,如果没有,请通知它们并取消选中。但是,这使我陷入困境,因为它再次触发了变化!我该如何来这个?
$('#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();
}
});
我不能解开绑架,然后重新定位更改事件,因为还有另一个插件依赖于此事件。
我还有什么其他选择?我可以将功能调用附加到每个复选框,但我希望有一些更优雅的东西,如果没有,我会默认情况下。
感谢所有的帮助
解决方案
您可以使用 .trigger()
要将其他参数传递给您的处理程序,我们将使用 loop
. 。如果它是 false
, ,不要再次运行另一元素触发器,这样:
$('#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);
}
});
其他提示
最简单的变化就是这样:
var inClickHandler = false;
function OnChecked(evt) {
if (inClickHandler) {
inClickHandler = false;
return;
}
inClickHandler = true;
// Do your stuff.
}
你为什么要打电话? $('#other_stages :checkbox:not(#section_2_active, #co_t)').change();
据我所知,您不需要这个,然后不会循环。复选框仍将检查,除非您使用 event.preventDefault();
和 .attr('checked',false);
不会触发变化,可能是因为您遇到的问题。
不隶属于 StackOverflow