我正在使用以下代码来获取单选按钮列表中所选元素的值。
function SelectRadioButton()
{
var radiobutton = document.getElementsByName('<%=RadioButtonList1.ClientID %>');
alert(radiobutton.length);
for(var x = 0; x < radiobutton.length; x++)
{
if(radiobutton[x].checked)
{
alert('selected is ' + radiobutton[x].id);
}
}
}
以下是 HTML 标记
<table id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1" class="chk" onclick="javascript:SelectRadioButton(this, ctl00_ContentPlaceHolder1_idControl_RadioButtonList1)" border="0">
<tr>
<td><input id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_0" type="radio" name="ctl00$ContentPlaceHolder1$idControl$RadioButtonList1" value="1" checked="checked" /><label for="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_0">List</label></td><td><input id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_1" type="radio" name="ctl00$ContentPlaceHolder1$idControl$RadioButtonList1" value="2" /><label for="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_1">Assignment</label>
但是我在alert(radiobutton.length);中得到长度0;陈述。 为什么会出现这种情况。我缺少什么吗?
最佳答案
您可以使用 jquery 来执行此操作。
alert($(".chk").find("input:checked").length); // chk is your css class name applied to Checkbox List element.
您可以使用此获取特定元素
alert($(".chk").find("input:checked")[0]);
关于javascript - 单选按钮在 javascript 中列出所选项目值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15269311/