使用jQuery each()函数循环遍历classname元素 [英] Using the jQuery each() function to loop through classname elements

查看:121
本文介绍了使用jQuery each()函数循环遍历classname元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery循环遍历具有相同类名和元素的元素列表。提取它们的值。

I am trying to use jQuery to loop through a list of elements that have the same classname & extract their values.

我有这个..

function calculate() {

    // Fix jQuery conflicts
    jQuery.noConflict();

    jQuery(document).ready(function(){    

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



    });    

}

我正在阅读each()函数在这种情况下如何正确使用它。

I was reading up on the each() function though got confused how to use it properly in this instance.

推荐答案

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

参考: .each() .val( ) 功能。

Reference: .each() and .val() functions.

这篇关于使用jQuery each()函数循环遍历classname元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆