使用jQuery Validate plugin时应该在哪里定义form action/URL?
Where should form action/URL be defined when using jQuery Validate plugin?
使用jQuery验证插件时应该在哪里定义表单动作?
在JavaScript?
还是在表格中?
或者两者兼而有之?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
//Links to script go here...
<script type="text/javascript">
$(function(){
var validator=$("#myForm").validate({
rules: {},
messages: {},
submitHandler: function(form) {
$.post('some/url/to/post/to.php',$(form).serializeArray(),function (json){
//Or maybe use form.action as url? Gives full URL: http://subdomain.example.com/some/url/to/post/to.php
//Or maybe use $(form).attr('action') as url? Gives partial URL: some/url/to/post/to.php
//bla bla bla
},'json');
}
});
});
</script>
</head>
<body>
<form id="myForm" action="some/url/to/post/to.php" method="post">
<input type="text" name="bla">
<input type="submit" value="sumbit">
</form>
</body>
</html>
Where should form action be defined when using jQuery Validate plugin?
这完全取决于您将如何提交表单。
1.常规表单提交,它被重定向到另一个页面:
只需在 form
标签中使用 action
属性,您就不需要使用自定义 submitHandler
选项。插件自动处理一切,验证后,表单正常提交。
2。通过 ajax 提交,它停留在同一页面上:
只需在自定义 submitHandler
函数中定义 ajax,您甚至不需要使用 action
属性,因为它无论如何都会被忽略。该插件会自动处理所有事情,并会在表单有效且单击按钮时触发自定义 submitHandler
函数。 (注意:检索 action
属性的值供 ajax 使用不会有问题。)
使用jQuery验证插件时应该在哪里定义表单动作?
在JavaScript?
还是在表格中?
或者两者兼而有之?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
//Links to script go here...
<script type="text/javascript">
$(function(){
var validator=$("#myForm").validate({
rules: {},
messages: {},
submitHandler: function(form) {
$.post('some/url/to/post/to.php',$(form).serializeArray(),function (json){
//Or maybe use form.action as url? Gives full URL: http://subdomain.example.com/some/url/to/post/to.php
//Or maybe use $(form).attr('action') as url? Gives partial URL: some/url/to/post/to.php
//bla bla bla
},'json');
}
});
});
</script>
</head>
<body>
<form id="myForm" action="some/url/to/post/to.php" method="post">
<input type="text" name="bla">
<input type="submit" value="sumbit">
</form>
</body>
</html>
Where should form action be defined when using jQuery Validate plugin?
这完全取决于您将如何提交表单。
1.常规表单提交,它被重定向到另一个页面:
只需在 form
标签中使用 action
属性,您就不需要使用自定义 submitHandler
选项。插件自动处理一切,验证后,表单正常提交。
2。通过 ajax 提交,它停留在同一页面上:
只需在自定义 submitHandler
函数中定义 ajax,您甚至不需要使用 action
属性,因为它无论如何都会被忽略。该插件会自动处理所有事情,并会在表单有效且单击按钮时触发自定义 submitHandler
函数。 (注意:检索 action
属性的值供 ajax 使用不会有问题。)