jQuery 如何使用jQuery从数组中的元素中检索值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12206660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:25:51  来源:igfitidea点击:

How to retrieve value from elements in array using jQuery?

jqueryhtmlarrays

提问by Sushan Ghimire

I have multiple input fields like so:

我有多个输入字段,如下所示:

<input type="text" name="card[]">
<input type="text" name="card[]">
<input type="text" name="card[]">

Users can add or remove these fields as required, therefore the name of the fields is an array. To get length of the array, this works fine:

用户可以根据需要添加或删除这些字段,因此字段的名称是一个数组。要获取数组的长度,这可以正常工作:

var n = $("input[name^= 'card']").length;

How can I read value from the array?

如何从数组中读取值?

I've tried this which didn't work:

我试过这个没有用:

var n = $("input[name^='card']").length;
var array = $("input[name^='card']");
for(i=0;i<n;i++)
{
 card_value=  array[i].val();
 alert(card_value);
}

This didn't work either:

这也不起作用:

var n = $("input[name^='card']").length;

for(i=0;i<n;i++)
{
 card_value=  $("input[name^='card["+i+"]']").val();
 alert(card_value);
}

How can I read value from this array? Help!

如何从这个数组中读取值?帮助!

采纳答案by scrappedcola

You should use:

你应该使用:

card_value=  array.eq(i).val(); //gets jquery object at index i

or

或者

card_value=  array[i].value; //gets dom element at index i

回答by NewBee

Use map function

使用地图功能

var values = $("input[name^='card']").map(function (idx, ele) {
   return $(ele).val();
}).get();

回答by Explosion Pills

jQuerycollections have a built in iterator with .each:

jQuery集合有一个内置的迭代器.each

$("input[name^='card']").each(function () {
   console.log($(this).val());
}

回答by PeeHaa

You can just loop though the items:

您可以循环遍历这些项目:

$("input[name^='card']").each(function() {
    console.log($(this).val());
});

回答by Shmiddty

Your syntax is incorrect.

你的语法不正确。

card_value = $(array[i]).val();or card_value = array[i].value;

card_value = $(array[i]).val();或者 card_value = array[i].value;

array[i] is not a jQuery object (for some reason).

array[i] 不是 jQuery 对象(出于某种原因)。

Checking your browser's console can be helpful for things like this.

检查浏览器的控制台可能有助于解决此类问题。

回答by Mark Pieszak - Trilon.io

Use: http://jsfiddle.net/xH79d/

使用:http: //jsfiddle.net/xH79d/

var n = $("input[name^='card']").length;
var array = $("input[name^='card']");

for(i=0; i < n; i++) {

   // use .eq() within a jQuery object to navigate it by Index

   card_value = array.eq(i).attr('name'); // I'm assuming you wanted -name-
   // otherwise it'd be .eq(i).val(); (if you wanted the text value)
   alert(card_value);
}

?

?

回答by R T

to read an array, you can also utilize "each" method of jQuery:

要阅读array,您还可以使用“ each”方法jQuery

$.each($("input[name^='card']"), function(index, val){
    console.log(index + " : " + val);
});

bonus: you can also read objects through this method.

奖励:您还可以通过此方法读取对象。

source

来源