首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JavaScript获取SelectedValue ASP.NET RadiobuttonList

使用JavaScript获取SelectedValue ASP.NET RadiobuttonList
EN

Stack Overflow用户
提问于 2017-11-29 01:53:28
回答 3查看 2.4K关注 0票数 1

在.aspx页面上有以下单选按钮列表:

代码语言:javascript
运行
复制
<asp:RadioButtonList ID="rbList" runat="server">
  <asp:ListItem Text="I accept" Value="accept" />
  <asp:ListItem Text="I decline" Value="decline" Selected="True" />
</asp:asp:RadioButtonList>

默认情况下,选择第二台收音机。是否有一种方法可以确定用户是否还没有选择第一个选项,即在执行操作时仍然选择“拒绝”?

例如:

代码语言:javascript
运行
复制
function checkRbList() {
  var rbl = document.getElementById(<%= rbList.ClientID %>);

  //if "decline" is still selected, alert('You chose to decline')...

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-11-29 02:04:59

下列人员应做好这项工作:

代码语言:javascript
运行
复制
var rbl = document.getElementById("<%= rbList.ClientID %>");    
var value = rbl.value;
if(value === 'decline')
    alert()
票数 2
EN

Stack Overflow用户

发布于 2017-11-29 02:22:49

假设您呈现了这个HTML:

代码语言:javascript
运行
复制
<label>
  I accept
  <input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
  I decline
  <input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>

您可以使用document.getElementsByName()。然后使用:

document.getElementsByName("rbList"),你会得到一个NodeList

这是一项功能:

代码语言:javascript
运行
复制
function checkRbList() {
  var rbl = document.getElementsByName("rbList"), len = rbl.length;

  for (var i = 0; i < len; i++) {
    if (rbl[i].checked) { // If checked?
      return rbl[i].value; // Returns the selected value.
    }
  }
}

要检查是否仍然选择了"decline"

代码语言:javascript
运行
复制
var targetValue = "decline";
if (checkRbList() === targetValue) {
  alert("You chose to decline.");
}

就像这样:

代码语言:javascript
运行
复制
(function() {

  var targetValue = "decline";

  function checkRbList() {
    var rbl = document.getElementsByName("rbList"),
      len = rbl.length;

    for (var i = 0; i < len; i++) {
      if (rbl[i].checked) { // If checked?
        return rbl[i].value; // Returns the selected value.
      }
    }
  }

  var btnValidate = document.getElementById("btnValidate");
  btnValidate.onclick = function() {
    console.log(checkRbList()); // Prints the selected value.
    if (checkRbList() === targetValue) {
      alert("You chose to decline.");
    }
  };

})();
代码语言:javascript
运行
复制
<label>
  I accept
  <input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
  I decline
  <input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>

<button id="btnValidate" type="button">Validate</button>

票数 2
EN

Stack Overflow用户

发布于 2017-11-29 16:04:33

我找到了一种行之有效的方法:

代码语言:javascript
运行
复制
var targetValue = "decline";
$('#<% = myBtn.ClientID %>').click(function () {
    var items = $("#<% = rbList.ClientID %> input:radio");
    for (var i = 0; i < items.length; i++) {
        if (items[i].value == targetValue) {
            if (items[i].checked) {
                alert(items[i].value);
            }
        }
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47543804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档