Javascript 检查同一类的所有输入值是否为空 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28494084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:52:14  来源:igfitidea点击:

check all input values of same class is empty jquery

javascriptjqueryhtmlcss

提问by tvshajeer

Here I have many input text fields with same class like

在这里,我有许多具有相同类的输入文本字段,例如

<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>
<input type="text" class="MyClass"></input>

My requirement is to check wheather all input fields of this class is empty or not. I've tried this

我的要求是检查这个类的所有输入字段是否为空。我试过这个

if(!('.MyClass').val()){
alert("empty");
}

But it doesn't make any result. Can anyone help?

但它不会产生任何结果。任何人都可以帮忙吗?

回答by Arun P Johny

You can check

你可以检查

$('button').click(function() {
  var $nonempty = $('.MyClass').filter(function() {
    return this.value != ''
  });

  if ($nonempty.length == 0) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>

Or using a flag

或者使用标志

$('button').click(function() {
  var flag = false;
  $('.MyClass').filter(function() {
    if (this.value != '') {
      flag = true;
      //no need to iterate further
      return false;
    }
  });

  if (!flag) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<input type="text" class="MyClass" />
<button>test</button>

回答by Milind Anantwar

You can compare the length of number of input with number of empty input using :

您可以使用以下方法将输入数量的长度与空输入的数量进行比较:

var allemptylength=$(".MyClass").filter(function() {
    return this.value.length !== 0;
})});

if($('.MyClass').length== allemptylength.length){
  alert("all empty");
}

Working Demo

工作演示

回答by slashsharp

To make sure each classes are checked, use

要确保检查每个类,请使用

$.each($('.MyClass'),function() {
   if ($(this).val().length == 0) {
    alert('empty');
   }
});

回答by dfsq

You can check it like this:

你可以这样检查:

var isEmpty = !$('.MyClass').filter(function() {
    return this.value.trim();
}).length;

Also remove all </input>tags. input elements are self-closable.

同时删除所有</input>标签。输入元件是可自闭合的。

回答by ronronronang

try this

尝试这个

    var hasNoValue;
    $('.MyClass').each(function(i) {
        if ($(this).val() == '') {
              hasNoValue = true;
        }
    });
    if (hasNoValue) {
        alert('All have no value');
    }

回答by Mateen

try is(':checked')

尝试 是(':检查')

$('.MyClass').each(function(){
    alert($(this).is(':checked');)
});

this will alert only the checked elements.

这只会提醒选中的元素。

回答by Cedrick Dayangco

Try this. It works for me

尝试这个。这个对我有用

var flag = 1;
$(".MyClass").each(function(i){
    if ($(this).val() == "")
        flag++;
});
if (flag == 1)
    alert('all have values');
else
    alert('some or all doesn't have value');

回答by Germán Augusto

$('input').each(function(index,value){
    if($(this).val()!=''){
        $(this).addClass('success');
    }else{
        $(this).removeClass('someClass');
        $(this).addClass('warning');
        $(this).css('border', '1px solid red');
        //do something else...
    }
});

回答by Waheed Asghar

Try this . 100% working..

尝试这个 。100% 工作..

var emptyLength = $(".MyClass").filter(function() {
    return this.value == "";
}).length;
 if (emptyLength > 0)
{
    alert("Please enter all peramerter's value");
    return;
}

Thanks.

谢谢。