Jquery多选择器返回错误的结果,而不是逐一执行

jquery multiple selector return wrong result rather than execute it one by one

本文关键字:执行 结果 选择器 返回 错误 Jquery      更新时间:2023-09-26

我有一个函数将返回真或假

$.fn.valForm = function(){
  if($(this).val().length < $(this).attr("minLength"))
  {
    alert("false");
  }
  else
  {
    alert("true");
  }
};    

执行multiselector (4)

$("#txtFirst, #txtLast, #txtNick, #txtEmail").valForm();

it result 1 time alert only

I need it result time alert like this

$("#txtFirst").valForm();
$("#txtLast").valForm();
$("#txtNick").valForm();
$("#txtEmail").valForm();

怎么做更简单?

你的插件不是用来处理这个的。

当你做插件时,总是添加return this.each,即:

$.fn.valForm = function() {
  return this.each(function() {
    if($(this).val().length < $(this).attr("minLength"))
    {
      alert("false");
    }
    else
    {
      alert("true");
    }
  });
};