基于输入类型的 jQuery 重载

发布于 2024-10-01 07:25:36 字数 276 浏览 0 评论 0原文

我想创建一个 jQuery 函数,可以将其附加到表单元素的 Change() 事件,但让该函数根据表单元素的类型(输入、选择、单选、密码)决定要执行的操作。

问题是,我不确定最好的方法 - 编写一个大型函数来决定内部要做什么,或者编写一种工厂函数,根据其类型将适当的函数附加到元素......我是在重新发明车轮?即在 jQuery 中是否有默认的方法来做这样的事情。

[附加:如果它影响决策,则需要在 load() 上调用该函数,以及在change() 上调用,并且能够应用/调用会很好吗?该功能仅一次]

I would like to create a jQuery function that I can attach to form elements change() events, but have the function decide what to do based on the type (input, select, radio, password) of form element.

Problem is, I'm not sure of the best approach - write a large function that decides internally what to do, or write a sort of factory function that attaches an appropriate function to the element based on its type... am I reinventing the wheel? i.e. is there a default way of doing something like this in jQuery.

[Additional: if it influences the decision, the function needs to be called on load(), as well as say on change(), and it would be nice to be able to apply/call? the function just the once]

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

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

发布评论

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

评论(2

我为君王 2024-10-08 07:25:36

您可以通过几个开关轻松地完成您想要的划分:

$.fn.myPlugin = function() {
   return this.each(function() {
     switch(this.nodeName) {
       case "INPUT":
         switch(this.type) {
           case "radio":
             $(this).change(function() { .... });
             break;
           case "text":
             $(this).change(function() { .... });
             break;
           /*password, etc....*/
         }
         break;
       case "SELECT":
         $(this).change(function() { .... });
         break;
       case "TEXTAREA":
         $(this).change(function() { .... });
         break;
     }         
   });
};

现在这是否是最好的方法肯定有争议......这取决于您是否对每个方法有独特的行为,或者其中一些行为共享,在这种情况下您可能希望在闭包中定义常用函数并引用它们,例如:

(function($) {
    function method1() { ... }
    function method2() { ... }
    function method3() { ... }

    $.fn.myPlugin = function() {
       return this.each(function() {
         switch(this.nodeName) {
           case "INPUT":
             switch(this.type) {
               case "radio":
                 $(this).change(method1);
                 break;
               case "text":
                 $(this).change(method3);
                 break;
               /*password, etc....*/
             }
             break;
           case "SELECT":
             $(this).change(method1);
             break;
           case "TEXTAREA":
             $(this).change(method2);
             break;
         }         
       });
    };    
})(jQuery);

您至少可以通过 20 种方式执行此操作,这只是一个建议,如果每个元素和类型都有特定的行为,这听起来 就像你问题中的情况一样。如果不是就忽略我:)

You can do what you want compartmentalized fairly easily with a few switches:

$.fn.myPlugin = function() {
   return this.each(function() {
     switch(this.nodeName) {
       case "INPUT":
         switch(this.type) {
           case "radio":
             $(this).change(function() { .... });
             break;
           case "text":
             $(this).change(function() { .... });
             break;
           /*password, etc....*/
         }
         break;
       case "SELECT":
         $(this).change(function() { .... });
         break;
       case "TEXTAREA":
         $(this).change(function() { .... });
         break;
     }         
   });
};

Now whether this is the best approach is certainly up for debate...it depends if you have a unique behavior for each, or some of them share behaviors, in which case you'll probably want to define common functions in a closure and reference those, for example:

(function($) {
    function method1() { ... }
    function method2() { ... }
    function method3() { ... }

    $.fn.myPlugin = function() {
       return this.each(function() {
         switch(this.nodeName) {
           case "INPUT":
             switch(this.type) {
               case "radio":
                 $(this).change(method1);
                 break;
               case "text":
                 $(this).change(method3);
                 break;
               /*password, etc....*/
             }
             break;
           case "SELECT":
             $(this).change(method1);
             break;
           case "TEXTAREA":
             $(this).change(method2);
             break;
         }         
       });
    };    
})(jQuery);

You could do this at least 20 ways, this is just a suggestion if each element and type has a specific behavior, which sounds like the case from your question. If it isn't just ignore me :)

ま昔日黯然 2024-10-08 07:25:36

我更喜欢在不同类型的输入上触发不同的jquery函数,然后调用一些其他函数(或以输入类型作为参数的相同javascript函数),或者像你说的那样编写一种工厂函数,可以根据更改的输入控件的类型决定采取什么操作。

I'd prefer to ether trigger different jquery functions on different types of input and then call some other functions (or the same javascript function with the type of input as a parameter), or to write like you say a type of factory function that can decide what action to take based on the type of input control thas was changed.

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