javascript - 单选按钮在 javascript 中列出所选项目值

标签 javascript asp.net

我正在使用以下代码来获取单选按钮列表中所选元素的值。

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/

相关文章:

javascript - 是否有与 AJAX 加载部分的页面等效的 document.ready ?

c# - 使用 EntityDataSource 和 WHERE 过滤数据

asp.net - 为什么 Asp.Net 中的 Session 对象在超时后没有被删除?

javascript - 使用 Javascript 更改相对 div 的左侧位置在 firefox/chrome 中不起作用

javascript - 为什么我的 .pagination li add active 不起作用?

javascript - 使用 jquery 创建在单击 "next/previous"时移动的响应式 slider ,但不使用插件

C# 虚拟和覆盖关键字

asp.net - IDataProtectionProvider 的目的字符串数组如何将默认值作为项目的物理路径?

javascript - Angularjs - 动态 ng-options

javascript - Grunt、Jasmine、Phantom、React 单元测试 : React throws on ReactElementValidator