Jquery和插件Validate,测试没有错误
我显然做了一些愚蠢的事情或者未能理解一些基本过程。很早以前就在玩这个。
当使用 onClick 方法单击提交按钮时,我试图检查正在验证的表单。
我正在使用 Jquery 和插件 Validate。我遇到的问题是在每个字段上进行验证,但是如果我单击提交而没有数据或未测试每个字段,我需要验证整个表单,在提交之前,我应该从 < 返回 false代码>validate().form()。这种情况不会发生,因为 submitForm()
中的 else
语句从未被执行。
在空表单上,单击提交后,会显示字段错误消息,但我对返回 false 的测试似乎不起作用。
$(document).ready(function() {
$('#formEnquiry').validate();
});
function submitForm() {
$('#msgid').append('<h1>Submitting Form (External Routine)</h1>');
if ($('#formEnquiry').validate().form()) {
$("#msgid").append("<h1>(Outside Ready) VALIDATED send to PHP</h1>");
}
else {
$('#msgid').append('<h1>(Outside Ready) NOT VALIDATED</h1>');
}
};
I have obviously done something stupid or failed to understand some fundamental process. Very early days playing with this.
I am trying to check for a form being validated, when the Submit Button is clicked with the onClick method.
<input class="submit" type="submit" value="Submit" onClick="submitForm()" />
I am using Jquery and the plug-in Validate. The problem I have is validating on each field is occurring, but if I click on submit with no data or not every field has been tested, I would need to validate the whole form, before submitting, I should get a return of false from validate().form()
. This is not occurring as the else
statement in submitForm()
is never being executed.
On an empty form, after clicking submit the field error messages are shown, but my testing of a return for false, does not seem to work.
$(document).ready(function() {
$('#formEnquiry').validate();
});
function submitForm() {
$('#msgid').append('<h1>Submitting Form (External Routine)</h1>');
if ($('#formEnquiry').validate().form()) {
$("#msgid").append("<h1>(Outside Ready) VALIDATED send to PHP</h1>");
}
else {
$('#msgid').append('<h1>(Outside Ready) NOT VALIDATED</h1>');
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ajax 的一个例子
});
你甚至不需要 val() 部分
你也可以在 ajax 之前在这个脚本中进行一些验证
这是 ajax 的基本示例(我正在使用 codeigniter,所以你可能需要使用有效的 URL 作为 url)
An example of Ajax
});
You dont really even need the val() part
You can also throw some validation into this script before the ajax
This is a basic example of ajax(I'm using codeigniter so you may need to use a valid URL for the url)