So I have a list with lots of items like below:
所以我有一个包含大量项目的列表,如下所示:
<ul id="usersList">
<li><FORM><INPUT class="eButton" type="button" value="robot669394444" onClick="openWin('robot669394444',1280,720)"></FORM></li>
<li><FORM><INPUT class="eButton" type="button" value="robot6693925" onClick="openWin('robot6693925',1280,720)"></FORM></li>
</ul>
I want to get all INPUT values using jQuery into an array. How to do such thing?
我想使用jQuery将所有INPUT值都放入数组中。怎么办这样的事?
3 个解决方案
#1
7
var vals = $("form input").map(function() {
return $(this).val();
});
Alternatively (more cleanly)
或者(更干净)
var vals = [];
$("form input").each(function() {
vals.push( $(this).val() );
});
The second alternative is more clean since it leaves you with a plain vanilla array. The result of map()
still is a jQuery object. Since these are (and behave exactly like) arrays, this might not be a problem. But it's useful to keep that subtle difference in mind.
第二种选择更干净,因为它给你留下了一个普通的香草阵列。 map()的结果仍然是一个jQuery对象。由于这些(和行为完全一样)数组,这可能不是问题。但是要记住这种微妙的差异是有用的。
#2
1
It's not always possible to store all params (keys and values) in an object, because two inputs can have same name.
并不总是可以将所有参数(键和值)存储在对象中,因为两个输入可以具有相同的名称。
But you can use: $('form.myform').serializeArray()
to get an object like [{param1:value2}, {param2: value2}]
但你可以使用:$('form.myform')。serializeArray()来获取像[{param1:value2},{param2:value2}]这样的对象
Or use $('form.myform').serializeArray().map(function(e){ return e.value;})
to get list of all values [value1, value2, ...]
或者使用$('form.myform')。serializeArray()。map(function(e){return e.value;})获取所有值的列表[value1,value2,...]
#3
1
I am not sure of a way to do it with Jquery, but using simple javascript can help
我不确定如何使用Jquery,但使用简单的JavaScript可以提供帮助
var uL=document.getElementById("usersList");
var i=0;
var inArr=new Array();
while(uL.getElementsByTagName("FORM")[i]){
inArr.push(uL.getElementsByTagName("FORM")[i].getElementsByTagName('input')[0]);
alert(inArr[i].value);
i++;
}
inArr
will contain all the input element objects in it...
inArr将包含其中的所有输入元素对象...
#1
7
var vals = $("form input").map(function() {
return $(this).val();
});
Alternatively (more cleanly)
或者(更干净)
var vals = [];
$("form input").each(function() {
vals.push( $(this).val() );
});
The second alternative is more clean since it leaves you with a plain vanilla array. The result of map()
still is a jQuery object. Since these are (and behave exactly like) arrays, this might not be a problem. But it's useful to keep that subtle difference in mind.
第二种选择更干净,因为它给你留下了一个普通的香草阵列。 map()的结果仍然是一个jQuery对象。由于这些(和行为完全一样)数组,这可能不是问题。但是要记住这种微妙的差异是有用的。
#2
1
It's not always possible to store all params (keys and values) in an object, because two inputs can have same name.
并不总是可以将所有参数(键和值)存储在对象中,因为两个输入可以具有相同的名称。
But you can use: $('form.myform').serializeArray()
to get an object like [{param1:value2}, {param2: value2}]
但你可以使用:$('form.myform')。serializeArray()来获取像[{param1:value2},{param2:value2}]这样的对象
Or use $('form.myform').serializeArray().map(function(e){ return e.value;})
to get list of all values [value1, value2, ...]
或者使用$('form.myform')。serializeArray()。map(function(e){return e.value;})获取所有值的列表[value1,value2,...]
#3
1
I am not sure of a way to do it with Jquery, but using simple javascript can help
我不确定如何使用Jquery,但使用简单的JavaScript可以提供帮助
var uL=document.getElementById("usersList");
var i=0;
var inArr=new Array();
while(uL.getElementsByTagName("FORM")[i]){
inArr.push(uL.getElementsByTagName("FORM")[i].getElementsByTagName('input')[0]);
alert(inArr[i].value);
i++;
}
inArr
will contain all the input element objects in it...
inArr将包含其中的所有输入元素对象...