我正在尝试进行简单的验证,其中包含 RadioButtonList rblstPallet
。我尝试了以下代码:
javascript
var rblstPallet = document.getElementById('rblstPallet');
var counter = 0;
for (var intCount = 0; intCount < rblstPallet.length; intCount++) {
if (rblstPallet[intCount].checked) { //this step is not working
console.log(intCount); //I checked using this step
counter++;
}
}
if (counter == 0) {
//MSG: please select any item
}
else {
// Redirect to next page function
}
.aspx
<asp:RadioButtonList ID="rblstPallet" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Wood</asp:ListItem>
<asp:ListItem>Plastic</asp:ListItem>
<asp:ListItem>None</asp:ListItem>
</asp:RadioButtonList>
问题是,如果我选择其中一个单选按钮,那么计数器值也保持不变。当我调试代码时,我发现该行
if (rblstPallet[intCount].checked) {
甚至没有执行,甚至没有在控制台中显示任何错误。我正在经历这个link 。我试过这个link (不工作)。
请帮忙。
最佳答案
RadioButtoList 转换为具有与 radiobuttonlist id 相似的 id 的单选按钮,您需要迭代
通过 DOM 来查找匹配
元素。
function getRadioButtonListSelections(radioButtonListName)
{
int selectionCount = 0;
for(i=0;i<document.forms[0].length;i++)
{
e=document.forms[0].elements[i];
if (e.id.indexOf(radioButtonListName) != -1 && e.checked)
selectionCount++;
}
return selectionCount;
}
alert(getRadioButtonListSelections('rblstPallet'));
关于javascript - 使用 javascript 检查 RadioButtonList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13911738/