I have a table in HTML with n number
of rows with same sex radio buttons in 0th
position of <td>
. I want to store values of sex (either 1
or 0
) in an array. Here is sample code for 3-rows
.
HTML Code:
<table>
<tr><td>
Male <input type="radio" name="sex" value="0"/>
Female <input type="radio" name="sex" value="1"/>
</td></tr>
<tr><td>
Male <input type="radio" name="sex1" value="0"/>
Female <input type="radio" name="sex1" value="1"/>
</td></tr>
<tr><td>
Male <input type="radio" name="sex2" value="0"/>
Female <input type="radio" name="sex2" value="1"/>
</td></tr>
</table>
JQuery Code:
var get_sex = new Array();
$("table").find('tr').each(function (i) {
var $tds = $(this).find('td');
get_sex[i] = $tds.eq(0).text();
});
$("#result").html(get_sex);
Output I get:
Male Female,Male Female,Male Female
Output I want:
1,0,1
Where 1 is checked Male and 0 is checked Female
您应该使用val
而不是text
:
get_sex[i] = $tds.eq(0).val();
You can map() the values to an array
var get_sex = $('input[type="radio"]:checked').map(function() {
return this.value;
}).get();
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.