2014-02-20 61 views
1

asp.net这是我的Default.aspx页面的Javascript与单选按钮列表

   <td>Radio </td> 
       <td> 
       <asp:RadioButtonList ID="RadioButtonList1" runat="server" > 
        <asp:ListItem Text = "One" Value = "1"></asp:ListItem> 
        <asp:ListItem Text = "Two" Value = "2"></asp:ListItem> 
        <asp:ListItem Text = "Three" Value = "3"></asp:ListItem> 
       </asp:RadioButtonList> 
       </td> 
       <td><div id="Div1" style="color:Red;display:none"></div></td> 
      </tr> 

      <tr> 
       <td>Dropdown </td> 
       <td> 
        <asp:DropDownList ID="DropDownList1" runat="server" > 
         <asp:ListItem>Select</asp:ListItem> 
         <asp:ListItem>Male</asp:ListItem> 
         <asp:ListItem>Female</asp:ListItem> 
        </asp:DropDownList> 
       </td>     
      </tr> 
     </table>  
<br /> 
<center> 
     <asp:Button ID="Button1" runat="server" Text="Edit"/> 
     <asp:Button ID="Button2" runat="server" Text="DropDown" OnClientClick="return ValidateDropDown('DropDownList1');" /> 
     <asp:Button ID="Button3" runat="server" Text="RadioValidate" OnClientClick = "return ValRad(document.getElementById('RadioButtonList1'));" /> 
</center> 

这是我分离的JavaScript文件:

function ValRad(CntrlID) { 
      alert("Radio"); 
      var RB1 = CntrlID; 
      alert("1"); 
      var radio = RB1.getElementsByTagName("input"); 
      alert("2"); 
      var isChecked = false; 
      alert("3"); 
       for (var i = 0; i < radio.length; i++) { 
       if (radio[i].checked) { 
       isChecked = true; 
       break; 
       } 
     } 
     if (!isChecked) { 
     alert("Please select an item"); 
     } 
    return isChecked;  
    } 

我无法验证单选按钮列表以及下拉列表,请帮助我... (SORRY格式不正确,我是新成员)请帮助我

回答

0

对于单选按钮列表您还可以使用RequiredFieldValidator

<asp:RadioButtonList ID="rblChoose" runat="server" RepeatDirection="Horizontal" 
RepeatLayout="Flow"> 
    <asp:ListItem Text="a" Value="a" /> 
    <asp:ListItem Text="b" Value="b" /> 
</asp:RadioButtonList> 
<asp:RequiredFieldValidator ID="rfvChoose" runat="server" 
ControlToValidate="rblChoose" ErrorMessage="Choose option." 
ForeColor="red" SetFocusOnError="true"></asp:RequiredFieldValidator> 

对于DropDown

<asp:DropDownList runat="server" id="ddl"> 
<asp:ListItem Value="0" text="Select a Value"> 
.... 
</asp:DropDownList> 

<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="ddl" InitialValue="Please select" ErrorMessage="Please select something" /> 
+0

RequiredFieldValidator的是服务器端控制, 我使用javascript – mmj89

+0

是其口味的问题想在客户端;)@ user3166530 –

+0

请尽快帮助我。谢谢 它没有asp服务器端控件。 – mmj89