我正在尝试让jQuery validate与我的单选按钮表单一起工作。然而,我在让它工作时遇到了问题,验证功能根本不起作用,这是我正在使用的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#formq").validate();
});
</script>
<style type="text/css">
<!--
label.error {display: none;}
//-->
</style>
</head>
<body>
<form id="formq" name="formq" action="check.php" method="post">
<fieldset>
<input type="hidden" name="check_submit" value="1" />
<label for="radio-1" tabindex="1" class="circle">Circle</label>
<input type="radio" name="radio-button" id="radio-1" value="Circle" validate="required:true" />
<label for="radio-2" tabindex="2" class="pentagon">Pentagon</label>
<input type="radio" name="radio-button" id="radio-2" value="Pentagon" />
<label for="radio-3" tabindex="3" class="triangle">Triangle</label>
<input type="radio" name="radio-button" id="radio-3" value="Triangle" />
<label for="radio-4" tabindex="4" class="hexagon">Hexagon</label>
<input type="radio" name="radio-button" id="radio-4" value="Hexagon" />
<label for="radio-5" tabindex="5" class="square">Square</label>
<input type="radio" name="radio-button" id="radio-5" value="Square" />
<label for="radio-6" tabindex="6" class="octagon">Octagon</label>
<input type="radio" name="radio-button" id="radio-6" value="Octagon" />
<label for="radio" class="error">Please select your gender</label>
<input class="submit" type="submit" value="Submit"/>
</fieldset>
</form>
</body>
</html>发布于 2011-04-20 01:13:30
带有“-”的名称不起作用。我没有检查文档,但是下面的代码工作正常
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }//gives an alert messagem when submitted
});
$(document).ready(function() {
$("#formq").validate({
rules: {
radiobutton: "required"
},
messages: {//to use custom messages
radiobutton: "This is needed, buddy!"
}
});
});如果您使用以前的方法,$("#formq").validate(),您也可以使用类:
<input type="radio" class="required" />发布于 2011-04-19 21:59:45
您尚未为jQuery验证指定任何规则。使用CSS、jquery元数据或在函数调用中执行此操作。例如(未测试):
$("#formq").validate({ rules:
{
"radio-button" : { required : true }
}
});发布于 2011-04-20 00:05:33
使用此选项,您可以测试是否已选中以及选中了哪一个:
$('.submit').bind('click',function() {
if($("#formq input:radio").is(':checked')) {
alert('true, value = '+$('#formq input:radio:checked').val());
} else {
alert('false');
}
});https://stackoverflow.com/questions/5717401
复制相似问题