【问题标题】:Use JavaScript to Get SelectedValue ASP.NET RadiobuttonList使用 JavaScript 获取 SelectedValue ASP.NET RadiobuttonList
【发布时间】:2019-10-02 03:27:33
【问题描述】:

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

<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>

默认选择第二个收音机。有没有办法让我确定用户是否没有选择第一个选项,即当他们执行操作时仍然选择“拒绝”?

例如:

function checkRbList() {
  var rbl = document.getElementById(<%= rbList.ClientID %>);

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

}

【问题讨论】:

    标签: javascript c# asp.net radiobuttonlist


    【解决方案1】:

    以下应该可以完成工作:

    var rbl = document.getElementById("<%= rbList.ClientID %>");    
    var value = rbl.value;
    if(value === 'decline')
        alert()
    

    【讨论】:

    • ClientID 周围缺少撇号
    【解决方案2】:

    假设你已经渲染了这个 HTML:

    <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

    这是函数:

    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"是否仍被选中:

    var targetValue = "decline";
    if (checkRbList() === targetValue) {
      alert("You chose to decline.");
    }
    

    类似这样的:

    (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.");
        }
      };
    
    })();
    <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>

    【讨论】:

      【解决方案3】:

      我找到了一种可行的方法:

      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);
                  }
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2010-10-09
        • 1970-01-01
        • 1970-01-01
        • 2012-03-13
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 2017-03-07
        相关资源
        最近更新 更多