How in a simples way would I test, if any of selected inputs is empty?
如果选择的任何输入都是空的,那么如何以简单的方式进行测试呢?
I get my inputs this way:
我的输入是这样的:
$myinputs = $('.inputstotest');
and would test single input with:
测试单个输入:
if ( $.trim($this.val()) == '' )
but how to test a group and return true if all are not empty or false if any (at least one) is empty?
但是,如果一个组(至少一个)为空,那么如何测试一个组并返回true ?
6 个解决方案
#1
6
I'd suggest:
我建议:
var someEmpty = $('.inputstotest').filter(function(){
return $.trim(this.value).length === 0;
}).length > 0;
With the above someEmpty
will be a Boolean, true
if there are inputs
of that class-name with a trimmed-value
equal to zero, false
if no inputs of that class-name have a trimmed-value
of a length equal to zero.
上面的某个空值将是一个布尔值,如果这个类名的输入值为零,则为false,如果这个类名的输入没有一个长度为零的trimmed值,则为false。
References:
引用:
-
filter()
. - filter()。
#2
2
You can do
你可以做
var $myinputs = $('.inputstotest');
if($myinputs.filter(function() { return $.trim(this.value) == ''; }).length > 0) {
//There is at least one empty input
}
#3
2
var invalidInputs = $('.inputstotest').filter(function() {
return $.trim(this.value) == '';
});
if (invalidInputs.length > 0) {
//some were invalid
invalidInputs.after('<span>Please enter a value!</span>');
}
#4
2
var result = true;
$('.inputstotest').each(function() {
if ($.trim($(this).val())) == '') {
result = false;
return false; // Terminate the .each loop
}
});
return result;
#5
0
What if you had each input have the same class eg. class="checkInput"
the used jquery to cycle through them using the code below:
如果每个输入都有相同的类呢?class="checkInput"使用的jquery循环使用:
var anyBlank = false;
$('.checkInput').each(function(){
if($(this).val() == ''){
anyBlank = true;
}
});
if(anyBlank){
alert('There is at least 1 input that is blank');
}
#6
0
You can use jQuery each function
您可以使用jQuery每个函数
$(".inputstotest").each(function(){});
Heres a fiddle for you: http://jsfiddle.net/Beyyd/1/
这里有一个小提琴给你:http://jsfiddle.net/Beyyd/1/
#1
6
I'd suggest:
我建议:
var someEmpty = $('.inputstotest').filter(function(){
return $.trim(this.value).length === 0;
}).length > 0;
With the above someEmpty
will be a Boolean, true
if there are inputs
of that class-name with a trimmed-value
equal to zero, false
if no inputs of that class-name have a trimmed-value
of a length equal to zero.
上面的某个空值将是一个布尔值,如果这个类名的输入值为零,则为false,如果这个类名的输入没有一个长度为零的trimmed值,则为false。
References:
引用:
-
filter()
. - filter()。
#2
2
You can do
你可以做
var $myinputs = $('.inputstotest');
if($myinputs.filter(function() { return $.trim(this.value) == ''; }).length > 0) {
//There is at least one empty input
}
#3
2
var invalidInputs = $('.inputstotest').filter(function() {
return $.trim(this.value) == '';
});
if (invalidInputs.length > 0) {
//some were invalid
invalidInputs.after('<span>Please enter a value!</span>');
}
#4
2
var result = true;
$('.inputstotest').each(function() {
if ($.trim($(this).val())) == '') {
result = false;
return false; // Terminate the .each loop
}
});
return result;
#5
0
What if you had each input have the same class eg. class="checkInput"
the used jquery to cycle through them using the code below:
如果每个输入都有相同的类呢?class="checkInput"使用的jquery循环使用:
var anyBlank = false;
$('.checkInput').each(function(){
if($(this).val() == ''){
anyBlank = true;
}
});
if(anyBlank){
alert('There is at least 1 input that is blank');
}
#6
0
You can use jQuery each function
您可以使用jQuery每个函数
$(".inputstotest").each(function(){});
Heres a fiddle for you: http://jsfiddle.net/Beyyd/1/
这里有一个小提琴给你:http://jsfiddle.net/Beyyd/1/