JQuery 更改事件循环
我有几个复选框。如果用户检查其中任何一项,我必须查看他们是否被允许这样做,如果不允许,请通知他们并取消选中。然而,这让我陷入了循环,因为它再次触发了变化!我怎样才能克服这个?
$('#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();
}
});
我无法取消绑定然后重新绑定更改事件,因为还有另一个插件依赖于该事件。
我还有什么其他选择?我可以将函数调用附加到每个复选框,但我希望有一些更优雅的东西,如果没有,我将默认这样做。
感谢大家的帮助
I have several checkboxes. If a user checks any of them, I have to see if they are allowed to do that, if not, notify them and uncheck it. However, this get me in a loop as it triggers the change again! How can I over come this?
$('#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();
}
});
I can not unbind and then rebind the change event as there is another plugin that is reliant on this event.
What other options do I have? I can attach a function call to each checkbox but I am hoping for something a bit more elegant, if not, I will default to this.
Thanks all for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.trigger()
将附加参数传递给处理程序,我们将使用循环
。如果为false
,则不要再次运行 other-element 触发器,如下所示:You can use
.trigger()
to pass an additional parameter to your handler, we'll useloop
. If it'sfalse
, don't run the other-element trigger again, like this:最简单的改变是这样的:
The simplest change is something like this:
为什么叫这条线?
$('#other_stages :checkbox:not(#section_2_active, #co_t)').change();
据我所知,您不需要这个,它也不会循环。除非您使用event.preventDefault();
,否则该复选框仍会选中,并且.attr('checked',false);
不会触发更改,可能是因为你遇到的问题。Why do you call this line?
$('#other_stages :checkbox:not(#section_2_active, #co_t)').change();
As far as I can see you don't need this and it won't then loop. The checkbox will still check unless you useevent.preventDefault();
and the.attr('checked',false);
won't trigger change, probably because of the issues you're having.