检查同一类的所有输入值是否为空jquery [英] check all input values of same class is empty jquery
本文介绍了检查同一类的所有输入值是否为空jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里,我有许多输入文本字段具有相同的类,如
< input type =textclass =MyClass >< /输入>
< input type =textclass =MyClass>< / input>
< input type =textclass =MyClass>< / input>
我的要求是检查这个类的所有输入字段是否为空。
我试过了这个
if(!('。MyClass')。val()){
alert(空);
}
但它不会产生任何结果。任何人都可以帮忙吗?
解决方案
您可以检查
<$ c $($。')'; $(')'。click(function(){var $ nonempty = $('。MyClass')。filter(function(){return this.value!=''}); if($ nonempty。
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script>< input type =text class =MyClass/>< input type =textclass =MyClass/>< input type =textclass =MyClass/>< button> test< / button> / code>
或使用标记
$(按钮')。click(function(){var flag = false; $('。MyClass')。filter(function(){if(this.value!=''){flag = true; //不需要进一步迭代返回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 =textclass =MyClass/>< input type =textclass =MyClass/>< button> test< / button> / code>
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?
解决方案
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>
这篇关于检查同一类的所有输入值是否为空jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文