【问题标题】:validation of radiobuttonlist in asp.net using javascript使用javascript验证asp.net中的radiobuttonlist
【发布时间】:2014-06-22 08:36:12
【问题描述】:

我必须使用 javascript 验证单选按钮。如果没有选择任何选项,它应该显示一条消息。

我的单选按钮代码是:

<asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem></asp:ListItem>
    </asp:RadioButtonList>

提交按钮是:

<asp:Button ID="next2" runat="server" Text=">>" Width="46px" 
        OnClientClick="return next2()"/>

对应的javascript函数是:

        function next2() {

        var listItemArray = document.getElementById('<%=welldata.ClientID %>');
        var isItemChecked = false;
        var length1 = listItemArray.length;

        for (var i=0; i<length1; i++)
          {
              var listItem = listItemArray[i];
              document.write(listItem);

             if ( listItem.checked )
                 {
               alert(listItem.value);
               isItemChecked = true;
                 }
           }

            if ( isItemChecked == false )
              {
              alert('Nothing is checked for welldata!');

              return false;
                 }

             return true;
    }

在调试时我注意到该函数已执行但未进入 for 循环。 我也试过了

        document.write(isItemChecked);
        document.write(listItemArray);
        document.write(length1);

输出是:

false [对象 HTMLTableElement] 未定义

【问题讨论】:

    标签: javascript asp.net


    【解决方案1】:

    wellDataRadioButtonListASP.NET 服务器控件将在浏览器中呈现为一个表格,其下有许多input type="radio" 控件,每个控件都具有相同的name

    所以,你需要先获取table标签内的input标签:

    var wellData = document.getElementById('<%=welldata.ClientID %>');
    var listItemArray = wellData.getElementsByTagName('input');
    

    当然,如果您出于某种奇怪的原因手动执行此操作。您可以使用RequiredFieldValidator 控件自动执行此操作。

    <asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem></asp:ListItem>
    </asp:RadioButtonList>
    <asp:RequiredFieldValidator ID="rfvWellData" runat="server" ControlToValidate="wellData" Display="Dynamic" ErrorMessage="Pick yourself some well data" />
    

    【讨论】:

      【解决方案2】:

      您正在使用默认情况下呈现为表格的 RadiobuttonList。如果您通过 id document.getElementById('&lt;%=welldata.ClientID %&gt;') 选择 dom 元素,那么您选择的是表格而不是 RadioButtonList。如果要选择单选按钮,则必须遍历 listItemArray 的子节点以获取单选按钮。或者,您可以使用 jquery 选择单选按钮,因为它们的 id 将以单选按钮列表的 id 开头(它们看起来像 welldata_0welldata_1)。

      这行 jquery 代码将获取您的单选按钮

      var listItemArray = $("input[id^='welldata']")
      

      【讨论】:

      • 您不希望使用带有硬编码 ID 的“开始于”查询,因为如果您在母版页内,ContentPlaceHolder 标记的 ID 将被添加到所有的 ID标签。你会想使用类似&lt;%= String.Format("$(\"input[id^='{0}']\")", wellData.ClientID) %&gt;
      • 如果是用户控件的母版页,我宁愿使用$("input[id*='welldata_']")
      • 对不起兄弟。没有 jQuery
      【解决方案3】:

      试试这个:

      function next2() {
      
              if(document.getElementById('<%=welldata.ClientID %>').checked==false)
      {
      alert('Nothing is checked for welldata!');
      return false;
      }
      return true;
      }
                         or 
      

      使用RequiredFieldValidator

      <asp:RequiredFieldValidator   
                  ID="ReqiredFieldValidator1"  
                  runat="server"  
                  ControlToValidate="welldata"  
                  ErrorMessage="Select your welldata!"  
                  >  
      

      查看以下链接

      http://asp-net-example.blogspot.in/2009/02/aspnet-requiredfieldvalidator-example.html

      http://www.itechies.net/tutorials/jscript/jsexample.php-pid-jform.htm#rb

      http://www.codeproject.com/Questions/499602/RequiredplusFieldplusValidatorplusForplusRadioplus

      【讨论】:

      • document.getElementById('&lt;%=welldata.ClientID %&gt;') 选择呈现的表格而不是单选按钮。
      • 嗯。试试RequiredFieldValidator
      【解决方案4】:
      var rblItems = document.getElementById('<%=radioBtnListId.ClientID %>').getElementsByTagName('input');
      
      var checkedItems = true;
      for (var i = 0; i < rblItems.length; i++) {
          if (rblItems[i].checked) {
              checkedItems = true;
              break;
          }
          else {
              checkedItems = false;
          }
      }
      
      if (!checkedItems) {//Hence No items checked
         alert("Nothing is checked");
         return false; 
      }
      

      【讨论】:

      • 如问题中所述,他/她想要验证单选按钮列表是否选中了任何选项。所以我通过使用 id 来获取单选按钮列表的属性。所以在 rblItems 变量中,我可以获得 rbl 中的所有选项。从那我使用for循环来检查每个选项是否被选中。如果选中任何选项,我将变量 checkedItems 设置为 true 并中断循环。那么如果checkedItems为真,我们可以发现其中一个选项被勾选了。希望这是有道理的。
      • 在答案中添加这个而不是评论。
      猜你喜欢
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多