开发者

How to iterate over two arrays in jQuery [duplicate]

This question already has answers here: Closed 11 years ago.

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜