将事件绑定到jQuery中的自定义插件函数

将事件绑定到jQuery中的自定义插件函数,jquery,events,plugins,bind,Jquery,Events,Plugins,Bind,我如何修改我的插件以允许在调用时加载事件?现在,当页面加载时插件正在加载,我希望它能够处理.blur()或任何我想分配给它的事件。如有任何帮助,将不胜感激: // The Plugin (function($) { $.fn.required = function() { return this.each(function() { var $this = $(this), $li = $this.closest("li"); if(!$this.val()

我如何修改我的插件以允许在调用时加载事件?现在,当页面加载时插件正在加载,我希望它能够处理.blur()或任何我想分配给它的事件。如有任何帮助,将不胜感激:

// The Plugin
(function($) {
  $.fn.required = function() {
    return this.each(function() {

      var $this = $(this), $li = $this.closest("li");
      if(!$this.val() || $this.val() == "- Select One -") {
        console.log('test');
        if (!$this.next(".validationError").length) {
          $li.addClass("errorBg");
          $this.after('<span class="validationError">err msg</span>');
        }
      } else if($this.val() && /required/.test($this.next().text()) === true) {
        $li.removeClass("errorBg");
        $this.next().remove();
      }

    });
  }
})(jQuery);

// The Event Call
$("[name$='_required']").required().blur();
//插件
(函数($){
$.fn.required=函数(){
返回此值。每个(函数(){
var$this=$(this),$li=$this.closest(“li”);
如果(!$this.val()| |$this.val()==“-选择一个-”){
console.log('test');
if(!$this.next(“.validationError”).length){
$li.addClass(“errorBg”);
$this.after('err msg');
}
}else if($this.val()&&/required/.test($this.next().text())==true){
$li.removeClass(“errorBg”);
$this.next().remove();
}
});
}
})(jQuery);
//事件调用
$(“[name$=''u required']”).required().blur();

它在blur()上不起作用,而是在加载文档时触发插件,而不是.blur()事件。

在Javascript中,当您在函数名后添加
()
时,它会立即执行。因此,当解释器遇到
(“[name$=''u required']”)。required().blur()
,它立即执行
required
,然后将返回值附加到
blur()
(这似乎不是您想要的)。试着这样做:

$("[name$='_required']").required.blur();
required
的实际函数对象绑定到
blur()
,并使其在该事件上执行。

(函数($){
(function($) { 
    $.fn.required = function() { 
        var handler = function() {
            var $this = $(this), $li = $this.closest("li"); 
            if(!$this.val() || $this.val() == "- Select One -") { 
              console.log('test'); 
              if (!$this.next(".validationError").length) { 
                $li.addClass("errorBg"); 
                $this.after('<span class="validationError">err msg</span>'); 
              } 
            } else if($this.val() && /required/.test($this.next().text()) === true) { 
              $li.removeClass("errorBg"); 
              $this.next().remove(); 
            } 
        };
        return this.each(function() {
            // Attach handler to blur event for each matched element:
            $(this).blur(handler);
        })
    } 
})(jQuery); 

// Set up plugin on $(document).ready:
$(function() {
    $("[name$='_required']").required();
})
$.fn.required=函数(){ var handler=function(){ var$this=$(this),$li=$this.closest(“li”); 如果(!$this.val() console.log('test'); 如果(!$this.next(“.validationError”).length){ $li.addClass(“errorBg”); $this.after('err msg'); } }else if($this.val()&&/required/.test($this.next().text())==true){ $li.removeClass(“errorBg”); $this.next().remove(); } }; 返回此值。每个(函数(){ //将处理程序附加到每个匹配元素的模糊事件: $(this.blur(handler); }) } })(jQuery); //在$(文档)上设置插件。准备就绪: $(函数(){ $(“[name$=''u required']”。required(); })