Leo*_*han 2 jquery jquery-plugins jquery-validate
这段代码是从另一个文件复制的,它在那个文件中工作,我检查了变量和表单 id,我确定名称与 js 代码一致。genFrom 中的文本框也可以验证工作,只有复选框不起作用。
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
$("#genForm").validate();
$("#genForm").validate( {
rules: {
agree: {
required: true
}
}
})
});
</script>
Run Code Online (Sandbox Code Playgroud)
<form id="genForm" method="post" action="verify.php">
<div class="container">
<div class="hero-unit">
<h1>Subscribe for final</h1>
</div>
<div class="clearfix">
<label>Email</label>
<div class="input">
<input name="Email" type="text" class='required'>
</div>
</div>
<br>
<br>
<div class="clearfix">
<label>Agreements:</label>
<div class="input">
<ul class="inputs-list">
<li>
I accept the terms and conditions.
<!-- Check box is here, not working -->
<input type="checkbox" name="agree" value="1">
</li>
</ul>
</div>
</div>
<br>
<input class="btn primary" type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
出于某种原因,您在validate()
同一个表单上调用了两次,您只需要一个:
// $("#genForm").validate(); No need for this
$("#genForm").validate( {
rules: {
agree: {
required: true
}
}
});
Run Code Online (Sandbox Code Playgroud)
演示:http : //jsfiddle.net/8MV5F/
validate()
不带参数的调用确实有一些(可配置的)默认行为,其中之一是将required
规则附加到带有 的元素class='required'
,这就是电子邮件字段工作的原因。您还可以required
为此使用该属性,此外,您可以使用type="email"
来验证电子邮件地址,而无需在规则中指定它。
这是一个简单的例子:http : //jsfiddle.net/8MV5F/2/
<form id="genForm" method="post" action="verify.php">
<label>Email:<input name="Email" type="email" required></label>
<label>I accept the terms and conditions.
<input type="checkbox" name="agree" value="1" required></label>
<input class="btn primary" type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
$("#genForm").validate();
Run Code Online (Sandbox Code Playgroud)
如果支持,这具有回退到本机浏览器验证的优势,以防 javascript 被禁用,甚至只是被破坏。