在 jQuery 中管理多个事件处理程序

发布于 01-04 05:34 字数 437 浏览 1 评论 0原文

我正在处理一个表单,当输入失去焦点时需要触发多个事件,即触发模糊事件。我在管理需要响应此事件的各种事件处理程序的调用顺序时遇到了麻烦。

每个表单输入标签都需要在模糊时发生各种事情。

  1. 各种自动格式化程序,例如确保名称以大写字母开头或根据复选框显示表单区域。
  2. jQuery 验证。
  3. 后验证功能,例如 ajax 调用

到目前为止,我想出的最好方法是依赖事件冒泡系统。因此,请确保使用 jQuery.on() 将第 1 点中的所有事件附加到比验证规则更深入 DOM 的元素。 Jquery 验证将事件侦听器附加到表单元素。

虽然这可行,但有几个问题。它适用于我拥有的 3 类事件,当我拥有更多事件时,它很可能会成为问题。

是否存在一种发布者/订阅者类型的系统,人们可以将其与 jQuery 一起使用,以使这种情况变得更容易?

I have a form that I'm working on that needs to have multiple events fired when an input loses focus, i.e. the blur event gets fired.I am having trouble managing what order the various event handlers that need to respond to this are called.

Each form input tag needs various things to happen to it on blur.

  1. Various auto formatters such as making sure the names start with a capital letter or showing areas of the form according to checkboxes.
  2. jQuery Validation.
  3. Post validation functions such as an ajax call

So far the best method I have come up with is to rely on the event bubbling system. Therefore, making sure that all the events from point 1 are attached using jQuery.on() to an element that is deeper into the DOM than the validation rules. Jquery validation attaches event listeners to the form element.

Whist this works it has a couple of problems. It works for the 3 classes of events I have it could well become a problem when I have more.

Is there a publisher/subscriber sort of system that people use with jQuery to make this sort of this easier?

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

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

发布评论

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

评论(1

絕版丫頭2025-01-11 05:34:30

您是否看过自定义事件并触发它们?

例如,您可以在模糊代码完成后创建自定义“getMeMore”事件:

$(document).on('blur', '.myInputClass', function(){
    // do stuff for blur here
    $(this).trigger('getMeMore');//now trigger my event
});
$(document).on('getMeMore','.myInputClass',function(){
    //do my ajax to get me more here
});

编辑:基于评论:

选择器中的文档只是实时事件的事件的锚点。请参阅此示例 http://jsfiddle.net/MarkSchultheiss/qM2Mv/,其中我在另一个中触发自定义事件基于条件的事件。观察数字(双倍的地方)以查看其他事件何时触发。我知道它是被解释的,但希望它能给你一些可以使用的东西。我在代码中执行此操作,以便我可以根据事件触发的给定时间可能存在或可能不存在的其他条件来管理“何时”事件触发。这允许将事件触发器与事件管理分离,这就是这里的重点。请注意我在哪里触发了来自不同位置的一些自定义事件。 (复选框值,并基于文本输入的长度)

Have you looked at custom events and firing those?

For example you could create a custom "getMeMore" event after the blur code is done:

$(document).on('blur', '.myInputClass', function(){
    // do stuff for blur here
    $(this).trigger('getMeMore');//now trigger my event
});
$(document).on('getMeMore','.myInputClass',function(){
    //do my ajax to get me more here
});

EDIT: based on comment:

The document there in the selector is simply an anchor for the event for live events. See this example http://jsfiddle.net/MarkSchultheiss/qM2Mv/ where I fire custom events within another event based on conditionals. Watch the numbers (where it doubles) to see when other events fire. I know it is construed but hopefully it would give you something to work with. I do this in my code so that I can manage "when" events fire based on other conditions which may or may not exist at a given time one event fires. This allows the separation of the event trigger from the event management which is the point here. Notice where I trigger some of the custom events from different places. (checkbox value, and based on the length of the text entry)

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