How to iterate over two arrays in jQuery [duplicate]
Possible Duplicate:
Simultaneously Iterating Over Two Sets of Elements in jQuery
I have two variables:
var $distance = $(".distance");
var $classification = $(".classification");
For each non-empty item of the $distance
collection I want to check if开发者_开发知识库 the corresponding $classification
item is not empty. How do I do this?
The question omits almost all key details, but it does sound like you want to do a 'zip' operation, like in this answer.
In this case, depending on how your HTML was laid out, something like this should work:
var bError = false
if ($distance.length != $classification.length) {
//--- Error!, length mismatch.
}
else {
$distance.each ( function (J, node) {
var distVal = $( node ).val (). trim ();
var classVal = $( $classification[J] ).val (). trim ();
if {!distVal || !classVal} {
bError = true;
return false;
}
} );
}
Are you meaning this?
if ($distance.length && $classification.length) {
//do some stuff
}
精彩评论