admin管理员组文章数量:1415139
I need get the same type of array that this.form.elements
returns, with input names as array keys.
I tried this.toArray()
, but even though it looks the same, it's missing the name keys (the keys are numbers).
Here's the full function:
(function( $ ) {
$.fn.blah = function(){
var that = this;
return this.each(function(){
$(this).bind('change', function(){
var matches1 = that.toArray(); // this doesn't work
var matches2 = this.form.elements; // this works.
console.log(matches1); // but both arrays look the same. wtf?
console.log(matches2);
return true;
}).change();
});
};
})(jQuery);
using it as $("input").blah();
I need get the same type of array that this.form.elements
returns, with input names as array keys.
I tried this.toArray()
, but even though it looks the same, it's missing the name keys (the keys are numbers).
Here's the full function:
(function( $ ) {
$.fn.blah = function(){
var that = this;
return this.each(function(){
$(this).bind('change', function(){
var matches1 = that.toArray(); // this doesn't work
var matches2 = this.form.elements; // this works.
console.log(matches1); // but both arrays look the same. wtf?
console.log(matches2);
return true;
}).change();
});
};
})(jQuery);
using it as $("input").blah();
-
that depends on what is the value of
this
. – Anurag Commented Jan 30, 2011 at 21:56 -
it's a set of selectors, es from
$("input").somefunction()....
– Alex Commented Jan 30, 2011 at 21:57
3 Answers
Reset to default 3In javascript you don't use an Array for named keys. You'd use an Object instead. I assume the values should be the elements themselves.
var result = {};
$.each(this.form.elements, function() {
result[ this.name ] = this;
});
Regarding your update, this line:
var matches1 = that.toArray();
...doesn't work because you're calling jQuery's toArray()
against a jQuery object.
If you wanted to use that, you'd call it from the library, and pass it the jQuery object.
var matches1 = $.toArray( that );
...although I don't know why you'd want to turn the jQuery object representing all <input>
element into an Array on each change
event.
This line:
var matches2 = this.form.elements; // this works.
... works because it is simply a reference to the elements in the form from the <input>
element that received the event. It uses native DOM API properties to get a collection (that isn't technically an Array).
Had to edit my answer because I was wrong about the use of toArray()
. I was thinking of makeArray()
. The toArray()
method is a wrapper for .get()
and does exactly the same thing.
Serialize is a possible choice. http://api.jquery./serializeArray/
$(this).serializeArray() rertuns it. :)
form.elements
returns an object of HTMLCollection
or HTMLFormControlsCollection
in HTML5.
The elements in these objects are accessible by index or name. So if you have a form with a single input element named "login", you could do:
form.elements.[0]
or
form.elements["login"]
to access the same element. You could simulate such an object by adding two entries for each form element with its corresponding index and name. But if form.elements
works for you, I'd remend using that directly.
本文标签: javascriptjQueryget array with input names as keysStack Overflow
版权声明:本文标题:javascript - jQuery - get array with input names as keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745227356a2648669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论