You could use filter
to reduce the set of all input
elements to only those that are empty, and check the length
property of what remains:
$(".next").click(function() {
var empty = $(this).parent().find("input").filter(function() {
return this.value === "";
});
if(empty.length) {
//At least one input is empty
}
});
Note that the definition of empty in the above code is an empty string. If you want to treat blank spaces as empty too, you may want to trim the value before comparing.
Also note that there is no need to pass this
into jQuery inside the filter
function. The DOM element itself will have a value
property, and it's much faster to access that instead of using val
.
Here's an updated fiddle.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…