我在客户端验证无线电按钮时遇到问题,名为“男性”,“女性”。我不想使用radiobutton列表,有人可以检查我的代码并纠正它吗?谢谢。
这是.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();
答案 0 :(得分:0)
我在asp中不受欢迎,但我知道你也可以添加HTML。如果您不想使用单选按钮,可以使用这样的选择框。
HTML
<select id = "gender">
<option value = 'm'>Male</option>
<option value = 'f'>Female</option>
</select>
JQUERY
<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>
答案 1 :(得分: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();" />