【发布时间】:2020-11-30 05:07:06
【问题描述】:
我正在尝试编写一个 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();
当我使用$(self).serialize(); 时,再次调用该函数。我希望“外部”部分仅在初始化时运行一次,而不是每次表单输入更改时运行一次。
我不明白这里发生了什么。如果有人能向我解释为什么会发生这种情况,我将不胜感激!
【问题讨论】:
-
问题是因为 jQuery 已经有一个
filter()方法并且你的代码造成了混乱。更改函数的名称,它可以正常工作:jsfiddle.net/RoryMcCrossan/3ba2L7ky
标签: javascript jquery jquery-plugins