检查所有输入是否为空(没有值)

Check if all inputs are empty (have no value)

我只是想快速组合一些东西来检查我的表单中是否有任何输入为空。
我正在尝试这个:

var isEmpty = true;
$('input').each(function(){
    if($(this).val() === ""){
        isEmpty = false;
    }
});
alert(isEmpty);

但它一直在提醒 true
如果我这样做:

$('input').each(function(){
    alert($(this).val();
});

它会提醒我的每个输入字段...
如果输入为空,为什么不返回 false?

这是一个小片段,用于检查您所有的 text inputs 是否为空:

var isEmpty = $("input[type='text']").filter(function () {
    return this.value.trim();
}).length === 0;

console.log( isEmpty );   // boolean true/false

jsBin playground

P.S:对于checkboxradio,游戏更简单:

var isUnchecked      = $("input[type='checkbox']:checked").length === 0;
var isRadioUnchecked = $("input[type='radio']:checked").length === 0;

奖励::blank 自定义选择器(适用于任何元素)

jQuery.extend(jQuery.expr[':'], {
  blank: function(e,i,m) {
    if(/input|textarea/i.test(e.tagName)) {
      if(/checkbox|radio/i.test(e.type)){
        return !e.checked;
      }else{
        return !e.value.length;
      }
    }else{
      return !e.innerHTML.trim();
    }
  }
});


$("div:blank").css({outline:"2px solid red"});
$("input:blank").css({outline:"2px solid red"});
$("textarea:blank").css({outline:"2px solid red"});
   
$("div:not(:blank)").css({outline:"2px solid green"}); // how cool is that? Using :not
*{margin:5px;padding:2px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div></div>
<div>Example using :not(:blank) selectors</div>
  
<input type="text" value="abc">
<input type="text" value="">
  
<input type="checkbox">
<input type="checkbox" checked>
  
<input type="radio" checked>
<input type="radio">
  
<textarea></textarea>
<textarea>abc</textarea>

<div><p> 等标准元素上,:blank 的行为不同于 jQuery 的 :empty 选择器,请注意空格和换行符:

 <p>

 </p>

并查看结果:

$("p:blank").length // 1 (found one blank paragraph)
$("p:empty").length // 0 there's no empty paragraph (since the newlines/spaces)

回到你的问题:)

由于您循环所有元素,目前您只将 isEmpty 的值设置为循环中的最后一个元素 - 值。
您可以将布尔标志断言到 if 中,例如:

var isEmpty = false; // start with a falsy presumption
$('input').each(function(){
    // as long as `isEmpty` is `false`
    if(isEmpty===false && $.trim( this.value ) === "") {
        isEmpty = true;
    }
});

此外,检查 空虚 === "" 对 return true

更有意义

否则反过来就是反转你的否定

var isEmpty = true; // start with a truthy presumption
$('input').each(function(){
    if(isEmpty && $.trim( this.value )){
        isEmpty = false;
    }
});

这是一个活生生的例子:

var isEmpty = true; // start with a truthy presumption
$('input').each(function(){
    if(isEmpty && $.trim( this.value )){
        isEmpty = false;
    }
});

alert(isEmpty); // false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="">
<input type="text" value="something">
<input type="text" value="">

在您的代码中,您正在检查:

if($(this) === "")

$(this) 是一个 jQuery 对象,它永远不会与空字符串相同。也许你打算写这个?

if ($(this).val() === "")

您需要检查每个输入的 val(),因此请记住获取要比较的值:

if ( $(this).val() === "" )

alert 在循环之外,所以它只会提醒最后的 value.Hope 下面的代码很有用

HTML

<input type="text" id=" " value="rdrdr"/>
<input type="text" id=" " value=" "/>
<input type="text" id=" " value="xdxd"/>
<input type="text" id=" " value=" "/>

JS

$('input').each(function(){
    var isEmpty = true;
    if($.trim($(this).val()) == ""){
        isEmpty = false;
    }
    console.log(isEmpty)
});

Chexk here for demo

尝试先初始化变量,不包含任何值。然后在一定范围内放置一个值并调用它。

var isEmpty;
    $('input').each(function(){
      if($(this) === ""){
        var isEmpty = false;
        return isEmpty;
        alert(isEmpty);
      } else {
        var isEmpty = true;
        return isEmpty;
        alert(isEmpty);
      }
    });