使用jQuery Validate插件时,form action/URL应该在哪里定义



使用jQuery Validate插件时form action应该在哪里定义?

在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>

当使用jQuery验证插件时,form动作应该在哪里定义?

这完全取决于你如何提交表单。

1。常规表单提交,重定向到另一个页面:

只需在form标记中使用action属性,就不需要使用自定义submitHandler选项。插件会自动处理所有的事情,验证后,表单会正常提交。

2。通过ajax提交,它保持在同一个页面上:

只需在自定义submitHandler函数中定义ajax,甚至不需要使用action属性,因为无论如何它都会被忽略。插件会自动处理一切,并在表单有效且单击按钮时触发自定义submitHandler函数。(注意:检索action属性的值供ajax使用不会有问题。)

最新更新