Check values of all fields of a certain class
In jQuery, how do I check the values of all option tags inside of a certain div.class to make sure they are开发者_Python百科 set?
var allSet = true;
$(".myClass select").each(function(i, el){
if($(":selected", el).length == 0){
allSet = false;
return false;
}
});
if(allSet == false){
alert("All selects are not set. :(");
} else {
alert("All selects are set. :)");
}
Assuming the value attributes are set to something:
<div class="myClass">
<select>
<option value="">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
$("div.myClass option[value='']").length === 0;
// => false
And if they're not:
<div class="myClass">
<select>
<option>One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div>
$("div.myClass option[value]").length === 0;
// => false
Of course, those two checks can be combined like so:
0 === $("div.myClass option[value='']").length +
$("div.myClass option[value]").length;
精彩评论