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()函数,虽然在这个例子中混淆了如何正确使用它。
1 个解决方案
#1
39
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()函数。
#1
39
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()函数。