web-dev-qa-db-ja.com

jQuery each()関数を使用してクラス名要素をループする

私はjQueryを使用して、同じクラス名を持つ要素のリストをループしてそれらの値を抽出しようとしています。

これ持ってる.

function calculate() {

    // Fix jQuery conflicts
    jQuery.noConflict();

    jQuery(document).ready(function(){    

        // Get all items with the calculate className
        var items = jQuery('.calculate');



    });    

}

このインスタンスで適切に使用する方法について混乱しましたが、私はeach()関数について読んでいました。

18
Brett
_jQuery('.calculate').each(function() {
    var currentElement = $(this);

    var value = currentElement.val(); // if it is an input/select/textarea field
    // TODO: do something with the value
});
_

そして、あなたがコレクションでそのインデックスを取得したい場合:

_jQuery('.calculate').each(function(index, currentElement) {
    ...
});
_

参照: .each() および .val() 関数。

48
Darin Dimitrov