jQuery get values from inputs and create an array

纵然是瞬间 提交于 2019-11-28 23:14:36

You can use .each() to iterate over the elements and add the names and values to a map (a plain object) like this:

var map = {};
$(".activeInput").each(function() {
    map[$(this).attr("name")] = $(this).val();
});

alert(map.key1); // "red"

See it in action.

Samuel Willems

I'm aware this question is four years old, and the accepted answer does suffice. Nevertheless I'd like to contribute an answer for the sake of completion, since this question is the first search result on Google.

jQuery has a map function as of version 1.2 (released in 2007). One could do something as simple as the following (this is not a cross-browser solution):

var $inputValues = $('.activeInput').map((i, el) => el.val());

Though nobody should ever use arrow functions in JavaScript, they are not supported in IE or Safari. The following is a cross-browser solution:

var $inputValues = $('.activeInput').map(function() {
    return $(this).val();
});

It is important to note that the return value is not a native array. This might cause problems when one would attempt to use methods which exist in Array.prototype. To convert the result to an array, one might do the following:

var inputValues = $('.activeInput').map(function() {
    return $(this).val();
}).toArray();

By running them through a loop you can create an object with string accessible values. Javascript doesn't have the concept of an associative array, but using bracket syntax you can access properties on an object in much the same way an associative array works in PHP.

var values = {};
$('.activeInput').each(function() {
    values[this.name] = this.value;
});

console.log(values['key1'], values['key3']);
// Note, this is the same as above.
console.log(values.key1, values.key3);

In your console you should see: red France

Here is a JsFiddle http://jsfiddle.net/rEtVt/ for it.

This is also referred to as a hashmap (concept) used for quick lookups.

var inputValues = new Array();
$('input').each(function(){
    inputValues[$(this).attr('name')] = $(this).val();
});

That is assuming, of course, that you want the value of all inputs.


That being said, many reasons not to use and Array have been brought to my attention.

If you need to serialize form for ajax submission then you should have a look at serialize and serializeArray jQuery methods. Special cases may occur when you have many inputs with the same name attribute that have to make array on the server.

Otherwise, I would call jquery serializeArray on the form element and iterate over its results to convert it into object.

UPD: added example http://jsfiddle.net/zQNUW/

Try this:

var values = new Object() // creates a new instance of an object
$('.activeInput').each(function() {
    values[$(this).attr('name')] = $(this).val()
})

To check the object properties:

output = ""
for (property in values) {
  output += property + ': ' + values[property]+'; ';
}
alert(output)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!