JQuery 更改事件循环

发布于 2024-09-28 07:23:36 字数 602 浏览 3 评论 0原文

我有几个复选框。如果用户检查其中任何一项,我必须查看他们是否被允许这样做,如果不允许,请通知他们并取消选中。然而,这让我陷入了循环,因为它再次触发了变化!我怎样才能克服这个?

$('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-10-05 07:23:36

您可以使用 .trigger() 将附加参数传递给处理程序,我们将使用循环。如果为 false,则不要再次运行 other-element 触发器,如下所示:

$('#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);
  }
});

You can use .trigger() to pass an additional parameter to your handler, we'll use loop. If it's false, don't run the other-element trigger again, like this:

$('#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);
  }
});
何以笙箫默 2024-10-05 07:23:36

最简单的改变是这样的:

var inClickHandler = false;

function OnChecked(evt) { 
  if (inClickHandler) {
    inClickHandler = false;
    return;
  }
  inClickHandler = true;

  // Do your stuff.
}

The simplest change is something like this:

var inClickHandler = false;

function OnChecked(evt) { 
  if (inClickHandler) {
    inClickHandler = false;
    return;
  }
  inClickHandler = true;

  // Do your stuff.
}
森末i 2024-10-05 07:23:36

为什么叫这条线? $('#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 use event.preventDefault(); and the .attr('checked',false); won't trigger change, probably because of the issues you're having.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文