简体   繁体   English

使用 jQuery each() 函数循环遍历类名元素

[英]Using the jQuery each() function to loop through classname elements

I am trying to use jQuery to loop through a list of elements that have the same classname & extract their values.我正在尝试使用 jQuery 遍历具有相同类名的元素列表并提取它们的值。

I have this..我有这个..

function calculate() {

    // Fix jQuery conflicts
    jQuery.noConflict();

    jQuery(document).ready(function(){    

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



    });    

}

I was reading up on the each() function though got confused how to use it properly in this instance.我正在阅读 each() 函数,但对如何在这种情况下正确使用它感到困惑。

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
});

and if you wanted to get its index in the collection:如果你想在集合中获取它的索引:

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

Reference: .each() and .val() functions.参考: .each().val()函数。

        $('.calculate').each(function(index, element) {
        $(this).text()
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM