jQuery 在 ajaxForm 之前验证
我尝试使用 jQuery ajaxForm 插件中的“beforeSubmit”选项来验证数据,但是即使存在无效的表单字段,表单也会提交。我哪里出错了?谢谢,
$(document).ready(function() {
function validator(){
$("#commentForm").validate();
}
$('#commentForm').ajaxForm({
dataType: 'json',
url: "http://highlandfamilyeyecare.com/contactengine.php",
beforeSubmit: validator,
success: function(data) {
$('ul.form').fadeOut("slow");
$('ul.form').html(data.formula).slideDown('slow');}
});
});
还有 html:
<ul class="form">
<li>
<form method="post" action="form.php" id="commentForm">
<label class="white">Your Name</label>
<input class="text-input required" type="text" name="name" /></li>
<li>
<label class="white">Email</label>
<input class="text-input required email" type="text" name="email"/></li>
<li>
<li><input type='submit' value="Submit" />
</form></li>
</ul>
I am trying to use the "beforeSubmit" option in the jQuery ajaxForm plugin to validate the data, however the form will submit even if there are invalid form fields. Where have I gone wrong? thanks,
$(document).ready(function() {
function validator(){
$("#commentForm").validate();
}
$('#commentForm').ajaxForm({
dataType: 'json',
url: "http://highlandfamilyeyecare.com/contactengine.php",
beforeSubmit: validator,
success: function(data) {
$('ul.form').fadeOut("slow");
$('ul.form').html(data.formula).slideDown('slow');}
});
});
And the html:
<ul class="form">
<li>
<form method="post" action="form.php" id="commentForm">
<label class="white">Your Name</label>
<input class="text-input required" type="text" name="name" /></li>
<li>
<label class="white">Email</label>
<input class="text-input required email" type="text" name="email"/></li>
<li>
<li><input type='submit' value="Submit" />
</form></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
beforeSubmit()
函数没有返回任何内容。您需要:
假设
$('#commentForm").validate()
存在并正确返回 true/false。The
beforeSubmit()
function isn't returning anything.You'll want:
presuming that the
$('#commentForm").validate()
exists and returns true/false correctly.根据文档,验证函数必须返回
false
以防止表单提交。Per the documentation, the validating function must return
false
in order to prevent form submission.假设
validate()
方法是验证插件,这是正确的做法。如果验证方法有任何错误,表单将不会提交。其中
ajaxFormOptions
是您的提交选项。Assuming that the
validate()
method is the validation plugin, This is the right way of doing it. If there are any errors in the validate method, form will not be submitted.where
ajaxFormOptions
are your options for submit.