我试过这段代码来验证单选按钮:
<html>
<head>
<script src="jquery-1.8.3.js" type="text/javascript">
</script>
<script src="jquery.validate.js" type="text/javascript">
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules:{
username:{
required:true,
alphanumeric:true
}
gender:"required",
},
messages:{
username:
{
required:"username cannot be empty",
alphanumeric:"username needs to be alphanumeric"
}
gender:{
required:"specify this field",
}
}
});
});
</script>
<style type="text/css">
label.error
{
color:red;
padding-left:.5em;
}
</style>
</head>
<body>
<form id="myform" action="r.html">
<table>
<tr>
<td><label for="username">Username<em>*</em>:</td></label><td><input type="text" id="username" name="username"/></td>
</tr>
<tr>
<td><label for="gender" validate="required:true" class="required">Gender<em>*</em>: </label></td><td><input type="radio" id="gender" name="gender" value="male"/>Male</td>
</tr>
<tr>
<td></td><td><input type="radio" id="gender" name="gender" value="female"/>Female</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
当我尝试此代码时,正在验证性别字段,但在性别和男性属性之间出现错误消息..但它必须出现0.5em
在男性属性之后。
并且字母数字验证根本不起作用。请有人帮帮我。提前致谢。