简体   繁体   中英

How to get collection of id of radio buttons contained in a div

I want o get a list of all the radio buttons that are contained in a div using javascript or jquery.

eg

<div id="container">
    <input type="radio" id="1">
    <input type="radio" id="2">
    <input type="radio" id="3">
     .... //total no of radio buttons are not known
</div>

I want an array containing an id of all radio buttons contained in div.

arr[0]="1"
arr[1]="2"
arr[2]="3"
...
..
var array = new Array();
$('#container input:radio').each(function (index) {
    array[index] = $(this).attr('id');
});
var ids = $('#container :radio').map(function() { return this.id; }).get();

This may work:

var arr= [];
$('#container :radio').each(function(){
 arr.push({ "the_id": this.id, "val":this.value })
});
//right now you have a json like array with ID and value and you can access it by `arr[0].the_id` and `arr[0].val`

You can only push the id (without value, so no curly bracket needed) and you can access elements like arr[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM