1

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

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

});

我无法取消绑定然后重新绑定更改事件,因为还有另一个依赖于此事件的插件。

我还有什么其他选择?我可以为每个复选框附加一个函数调用,但我希望有一些更优雅的东西,如果没有,我会默认使用这个。

谢谢大家的帮助

4

3 回答 3

2

您可以使用.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);
  }
});
于 2010-10-19T14:44:30.420 回答
0

最简单的更改是这样的:

var inClickHandler = false;

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

  // Do your stuff.
}
于 2010-10-19T14:41:33.057 回答
0

为什么叫这条线?$('#other_stages :checkbox:not(#section_2_active, #co_t)').change();据我所知,你不需要这个,它也不会循环。该复选框仍会检查,除非您使用event.preventDefault();并且.attr('checked',false);不会触发更改,可能是因为您遇到的问题。

于 2010-10-19T14:47:31.210 回答