0
<asp:RadioButtonList ID="rlist" runat="server"          
  style="z-index: 1; left: 194px; top: 69px; position: absolute;
  height: 21px; width: 110px" 
  RepeatDirection="Horizontal" Font-Bold="True" ForeColor="Black">
       <asp:ListItem Value="True">Yes</asp:ListItem>
       <asp:ListItem Value="False">No</asp:ListItem>
</asp:RadioButtonList>

这是我的编码,如果用户没有选择任何一个单选按钮意味着它将在 javascript 中显示警报

4

4 回答 4

2

使用验证器

   <asp:RequiredFieldValidator   
        ID="ReqiredFieldValidator1"  
        runat="server"  
        ControlToValidate="RadioButtonList1"  
        ErrorMessage="Select your choice!"  
        >  
    </asp:RequiredFieldValidator>  
于 2012-06-07T12:26:20.903 回答
1
Now it is Tested Pls change you txtbox Client Id and radiobutton ID IT fully Works OMG    



     <script type="text/javascript">
                    function buttonclick()
                    {
                        var txt1 = document.getElementById('<%=TextBox1.ClientID %>');
                        var txt2 = document.getElementById('<%=TextBox2.ClientID %>');


     var b = new Boolean(validateRadioButtonList('<%= rlist.ClientID %>'))
                         if (b == false)
                          {
                             return false;

                          }
                        if (txt1.value == "")
                        {
                            alert("please enter text1");
                            return false;
                        }
                        if (txt2.value == "")
                        {
                            alert("please enter text2");
                            return false;
                        }

                    }
                    function validateRadioButtonList(radioButtonListId)
                    {
                        var listItemArray = document.getElementsByName(radioButtonListId);
                        var isItemChecked = false;

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

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

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

                            return false;
                        }

                        return true;
                    }

                </script>
            </head>
            <body>
                <form id="form1" runat="server">
                <div>
                    <asp:RadioButtonList ID="rlist" runat="server" style="z-index: 1; left: 194px; top: 69px; position: absolute; height: 21px; width: 110px" RepeatDirection="Horizontal" Font-Bold="True" ForeColor="Black">
 <asp:ListItem Value="True">Yes</asp:ListItem> 
<asp:ListItem Value="False">No</asp:ListItem> 
</asp:RadioButtonList> 
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" OnClientClick="return buttonclick();" Text="Button" />
                </div>
                </form>
</body>
于 2012-06-07T12:36:55.497 回答
0

您可以使用 JQuery 来验证这一点。

该语句$('#RadioButtonList1 input:checked').val()将为您提供所选项目的文本,否则为空。

于 2012-06-07T12:28:59.960 回答
-1
<form id="Form1" method="post" runat="server">
 <asp:RadioButtonList id="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
  <asp:ListItem Text="Excellent" Value="1" />
  <asp:ListItem Text="Good" Value="2" />
  <asp:ListItem Text="Average" Value="3" />
  <asp:ListItem Text="Poor" Value="4" />
  <asp:ListItem Text="Very Poor" Value="5" />
 </asp:RadioButtonList>
 <input type="button" onclick="onButtonClick();" value="Validate">
</form>

<script type="text/javascript">
<!--
function onButtonClick()
{
 return validateRadioButtonList('<%= RadioButtonList1.ClientID %>');
}
function validateRadioButtonList(radioButtonListId)
{
 var listItemArray = document.getElementsByName(radioButtonListId);
 var isItemChecked = false;

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

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

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

  return false;
 }

 return true;
}
// -->
</script>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return onButtonClick() ;" onclick="btnSubmit_Click" />
于 2012-06-07T12:51:11.340 回答