0

我在验证客户端名为“男性”、“女性”的单选按钮时遇到问题。我不想使用单选按钮列表,有人可以检查我的代码并纠正它吗?谢谢。

这是 .aspx 页面:

<script language="javascript" type="text/javascript" >

                function ClientValidate(source, e) {
                    if (document.getElementById("content_radmale").checked == true || document.getElementById("content_radfemale").checked == true) {
                        e.IsValid = true;
                    }
                    else {
                        e.IsValid = false;
                    }
                  //  alert("Enter gender  !");
                }
</script>
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="please enter any option" ClientValidationFunction = "ClientValidate"
        ValidationGroup="gender" ControlToValidate="txtname"></asp:CustomValidator>

和 .cs 页面:

    string gender = radmale.Text;

    if (radfemale.Checked)
    {
        gender = radfemale.Text;

    }
    CustomValidator1.Validate(); 
    string sql = "INSERT INTO registration(emailaddress,username,password,name,gender)VALUES ('"+email+"','"+username+"','"+pass+"','"+name+"','"+gender+"')  ";
    SqlCommand cmd = new SqlCommand(sql, con);

    con.Open();

    cmd.ExecuteNonQuery();

   con.Close();
4

2 回答 2

0

我在 asp 中不受欢迎,但我知道你也可以在其中添加 html。如果您不想使用单选按钮,可以使用这样的选择框。

HTML

<select id = "gender">
   <option value = 'm'>Male</option>
   <option value = 'f'>Female</option>
</select>

查询

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type = "text/javascript">
   $(document).ready(function() { 
       //get the selected gender
      $('#gender').change(function() {
          var gender = $(this).val();
          alert(gender);
      });
   })
</script>
于 2013-06-25T06:19:59.070 回答
0

请尝试使用以下代码片段。

JS

    function validateRadioButton() {
        var v1 = document.getElementById("RequiredFieldValidator1");
        if ($('input:radio[name=Gender]').is(':checked') == false) {
            ValidatorEnable(v1, true);
            return false;
        }
        else {
            ValidatorEnable(v1, false);
            return true;
        }
    }

    function ClientValidate(source, e) {
        e.IsValid = false;
    }

    $(document).ready(function () {
        var v1 = document.getElementById("RequiredFieldValidator1");
        ValidatorEnable(v1, false);
    });

ASPX

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" Text="Male" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" Text="Femlae" />
<div style="display: none;">
    <asp:TextBox ID="TextBox1" runat="server" Text="1"></asp:TextBox>
</div>
<asp:CustomValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
    ClientValidationFunction="ClientValidate">jayesh goyani</asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validateRadioButton();" />
于 2013-06-25T08:34:46.503 回答