jQuery 没有表单标签的jQuery验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15231847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery validation without form tag
提问by Diego_sf93
I have a "form" with many inputs and at the submit button I call a javascript function that manipulate the info and send by ajax to a php file..
我有一个包含许多输入的“表单”,在提交按钮上,我调用了一个 javascript 函数来操作信息并通过 ajax 发送到一个 php 文件..
I can add a form tag without an action url to validate my "form" with the jquery validate? Or I have to do manually the validation?
我可以添加一个没有操作 url 的表单标签来验证我的“表单”与 jquery 验证?或者我必须手动进行验证?
Thanks!
谢谢!
采纳答案by Sparky
"I have a 'form' with many inputs and at the submit button I call a javascript function that manipulate the info and send by ajax to a php file"
“我有一个包含许多输入的‘表单’,在提交按钮上,我调用了一个 javascript 函数来操作信息并通过 ajax 发送到一个 php 文件”
It is absolutely required that you have <form></form>
tags for the jQuery Validate plugin to function properly, or at all.
绝对需要您拥有<form></form>
jQuery Validate 插件的标签才能正常运行,或者根本不需要。
If you use ajax
, you must put your ajax
inside the submitHandler
of the jQuery Validate plugin. See below for my example.
如果你使用ajax
,你必须把你的 jQuery Validate 插件放在ajax
里面submitHandler
。请参阅下面的示例。
"I can add a form tag without an action url to validate my "form" with the jquery validate?"
“我可以添加一个没有操作 url 的表单标签来使用 jquery 验证来验证我的“表单”?”
Yes, you can remove or block the action
and still use jQuery Validate.
是的,您可以删除或阻止action
并仍然使用 jQuery 验证。
See demo: http://jsfiddle.net/NEPsc/3/
见演示:http: //jsfiddle.net/NEPsc/3/
Put your ajax
and a return false
in the submitHandler
and no regular submission will occur:
将您的ajax
和 areturn false
放入submitHandler
,不会发生常规提交:
$(document).ready(function() {
$('#myform').validate({ // initialize plugin
// your rules & options,
submitHandler: function(form) {
// your ajax would go here
return false; // blocks regular submit since you have ajax
}
});
});
EDIT:
编辑:
As per OP's comments, to use a input type="button"
instead of a input type="submit"
, you'll need to use a click
handler. There is no need to have any inline JavaScript. Remove the onClick
function entirely.
根据 OP 的评论,要使用 ainput type="button"
而不是 a input type="submit"
,您需要使用click
处理程序。 不需要任何内联 JavaScript。onClick
完全删除该功能。
See updated demo: http://jsfiddle.net/NEPsc/5/
查看更新的演示:http: //jsfiddle.net/NEPsc/5/