javascript - 使用 javascript 检查 RadioButtonList

标签 javascript asp.net html

我正在尝试进行简单的验证,其中包含 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/

相关文章:

c# - 找不到路径的一部分 'D:\~\images\Emblem.JPG'

c# - 错误 : Excepiton in System. Threading.ThreadAbortException:线程被中止

c# - 在 asp.net mvc 中显示基于身份验证状态的 View

javascript - 将组件传递给 Angularjs 对话框模板时删除额外的 md-dialog 包装器

javascript - 异步函数在调用一次时有效,但多次调用时无效

javascript - .delete() 渲染错误路径

javascript - 如何从 Ajax() 的 innerHTML 缓存 JS 文件?

javascript - 哪个 Javascript 工具包降级得最优雅?

javascript - 函数参数的计算结果为未定义

CSS & DIV : Passing parameters with Div classes?