简体   繁体   English

jQuery Object 方法被多次调用

[英]jQuery Object Method Gets Called Multiple Times

I am trying to write a jQuery method which watches for changes of inputs inside a given form element:我正在尝试编写一个 jQuery 方法来监视给定表单元素内的输入变化:

(function($) {
    $.fn.filter = function(options) {
        console.log('Outside');

        var self = this;
        var settings = $.extend({}, options);        

        this.on('change', ':input', function(e) {
            console.log('Inside');

           $(self).serialize(); // Here is the problem

        });

        return this;
    }
})(jQuery);

$('#filter-form').filter();

When I use $(self).serialize();当我使用$(self).serialize(); , the function being called again. ,再次调用 function。 I expect that the 'Outside' part only runs once on initialization and not every time the input of the form changes.我希望“外部”部分仅在初始化时运行一次,而不是每次表单输入更改时。

I do not understand what is happening here.我不明白这里发生了什么。 I'd appreciate if someone could explain to me why this is happening!如果有人可以向我解释为什么会这样,我将不胜感激!

The issue is that you are redefining jQuery's filter method, which it internally uses in its serialize method.问题是您正在重新定义 jQuery 的filter方法,它在其serialize方法中内部使用。 If you change the name, it will work.如果您更改名称,它将起作用。 The definition of serialize is shown below: serialize的定义如下所示:

jQuery.fn.extend( {
    serialize: function() {
        return jQuery.param( this.serializeArray() );
    },
    serializeArray: function() {
        return this.map( function() {

            // Can add propHook for "elements" to filter or add form elements
            var elements = jQuery.prop( this, "elements" );
            return elements ? jQuery.makeArray( elements ) : this;
        } )
        .filter( function() {
            var type = this.type;

            // Use .is( ":disabled" ) so that fieldset[disabled] works
            return this.name && !jQuery( this ).is( ":disabled" ) &&
                rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
                ( this.checked || !rcheckableType.test( type ) );
        } )
        .map( function( _i, elem ) {
            var val = jQuery( this ).val();

            if ( val == null ) {
                return null;
            }

            if ( Array.isArray( val ) ) {
                return jQuery.map( val, function( val ) {
                    return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
                } );
            }

            return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
        } ).get();
    }
} );

Working Example:工作示例:

 (function($) { $.fn._filter = function(options) { console.log('Outside'); var self = this; var settings = $.extend({}, options); this.on('change', ':input', function(e) { console.log('Inside'); $(self).serialize(); }); return this; } })(jQuery); $('#filter-form')._filter();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="filter-form"> <input type="text"> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM