有没有一种方法可以在不使用全局变量的情况下打破 jQuery 中的事件递归?

发布于 2024-09-27 09:32:00 字数 1165 浏览 3 评论 0原文

我有一个带有 2 个文本输入和 2 个跨度控件的表单。通常,当文本框 A 更改时,会触发一个事件来更改范围 A,而当文本框 B 更改时,会触发一个事件来更改范围 B。

但是,在一种特殊情况下,我希望更改文本框 A 或文本框 B更新跨度 A 和 B。在这种情况下,我尝试将事件连接到相应的控件,但它不起作用,因为在事件构建代码中设置了很多状态(更不用说每个事件调用“this”) ',如果逻辑是从与预期不同的地方触发的,这将使逻辑使用错误的控制)。

为了使事情变得简单,最好在创建事件处理程序时将一个字符串(代表另一个文本输入 ID)传递给事件处理程序,然后在第二个控件上手动调用 change() 事件。然而,这会使事情陷入无限递归循环。我想到了一种绕过递归的方法,但它需要一个全局变量。

有没有比这更好的方法,最好是不需要全局变量的方法?

ml_inEvent = false;



   $ctlToVal.bind('keyup change', {feedbackCtl: ml_feedback, controlsToMonitor: ary, validationGroup: this.validationGroup, controlToFire: ctlToFire}, function(event) {
        // Other processing happens here...

        if (event.data.controlToFire != '') {
            var $controlToFire = $('#' + event.data.controlToFire);
            if ($controlToFire.length) {
                // Use a global variable to ensure this event is not fired again
                // as a result of calling the other one
                if (!ml_inEvent) {
                    ml_inEvent = true;
                    $controlToFire.change();
                    ml_inEvent = false;
                }
            }
        }
    });

I have a form with 2 text inputs and 2 span controls. Normally, when textbox A is changed an event is fired to change span A, and when textbox B is changed, an event is fired to change span B.

However, in one particualar case I would like a change either textbox A or textbox B to update both span A and B. I tried wiring the events up to the corresponding controls in this case, but it didn't work because there is much state that is set up in the event building code (not to mention each event calls 'this', which would make the logic use the wrong control if it were fired from a different one than it was intended).

To make things easy, it would be best to pass a string (representing the other text input id) to the event handler at the time it is created, and then calling the change() event manually on the second control. However, this puts things in an infinite loop of recursion. I thought of a way to get around the recursion, but it reqires a global variable.

Is there a better way than this, preferably one that doesn't require a global variable?

ml_inEvent = false;



   $ctlToVal.bind('keyup change', {feedbackCtl: ml_feedback, controlsToMonitor: ary, validationGroup: this.validationGroup, controlToFire: ctlToFire}, function(event) {
        // Other processing happens here...

        if (event.data.controlToFire != '') {
            var $controlToFire = $('#' + event.data.controlToFire);
            if ($controlToFire.length) {
                // Use a global variable to ensure this event is not fired again
                // as a result of calling the other one
                if (!ml_inEvent) {
                    ml_inEvent = true;
                    $controlToFire.change();
                    ml_inEvent = false;
                }
            }
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

三寸金莲 2024-10-04 09:32:00

您可以使用 .trigger() 上的 extraParameters 参数来突破,例如:

$ctlToVal.bind('keyup change', {feedbackCtl: ml_feedback, controlsToMonitor: ary, validationGroup: this.validationGroup, controlToFire: ctlToFire}, function(event, fire) {
  // Other processing happens here...
  if(fire !== false)  $('#' + event.data.controlToFire).trigger('change', false);
});

您可以在这里尝试。这样做的作用是事件处理程序回调不仅接收 事件 object 以及您传入的任何其他参数,在本例中我们只使用一个简单的 false 和一个 !=== 检查这一点很重要,因此 undefined (根本没有传递参数)仍然会更改两个控件。因此,例如 $("#control").change() 会更改两个控件,但仍然不循环...您可以在此处进行测试

You can use the extraParameters argument on .trigger() to break out, for example:

$ctlToVal.bind('keyup change', {feedbackCtl: ml_feedback, controlsToMonitor: ary, validationGroup: this.validationGroup, controlToFire: ctlToFire}, function(event, fire) {
  // Other processing happens here...
  if(fire !== false)  $('#' + event.data.controlToFire).trigger('change', false);
});

You can give it a try here. What this does is the event handler callback not only receives the event object but also any other arguments you pass in, in this case we're just using a simple false and a !=== check this in important so undefined (the parameter not passed at all) still changes both controls. So for instance $("#control").change() would change both controls, but still not loop...you can test that here.

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