2014-02-20 93 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