How to store an attribute of all input(checkbox) elements in Javascript?
I am trying to store the name attribute of all the checkboxes in a page, in some sort of array/data structure.
How do I go about doing this?
<input name="sam开发者_开发技巧ple" type="checkbox" value="" align="left" />
<input name="sample2" type="checkbox" value="" align="left" />
The name attribute will be unique. Any idea on how to go about doing this?
You can use .map()
to get properties of a set of elements and into an array, like this:
var arr = $("input[type=checkbox]").map(function() { return this.name; }).get();
There are slimmer selectors, like input:checkbox
or even :checkbox
, but they are much slower.
var store_them_here = [];
$(':checkbox').each(function(i, e) {
store_them_here.push($(e).attr('name'));
})
Try
var result_array = [];
jQuery(':checkbox').each( function(index, Element){
result_array.push(jQuery(this).attr('name'));
})
精彩评论