【问题标题】:redirect after .submit() form.submit() 表单后重定向
【发布时间】:2012-05-28 20:38:10
【问题描述】:

我有一个 HTML 表单,它使用 div (submitme) 来触发以下代码.....然后代码使用 bValidator 插件来验证表单字段,然后在提交表单之前检查蜜罐字段是否为空白....

当我排除“window.location ...”等时,.submit() 会起作用并通过它发布表单,但是在包含它时;验证/蜜罐仍然有效,但没有提交表单。

我是 JQUERY 的新手,想知道是否缺少一些简单的内容或更好的方法来编写以下内容:

$("#submitme").click(function(){
    if($('#right-body-div').data('bValidator').validate()) {

        if ($("#honeypot").val() == "") {
                $('#pgpost').submit();
                window.location = "http://www.homepage.co.uk/thankyou";    
                return true;
              }
           window.location = "http://www.homepage.co.uk/nothankyou";
           return false;
    }
});

如果可能,我想在不使用 AJAX/PHP 的情况下完成此操作。

您的想法将不胜感激。

【问题讨论】:

  • 理想情况下,您不会发布然后重定向,您只需发布即可。服务器应使用适当的响应来响应帖子。重定向不是必需的,因为发布行为会刷新到新页面。您应该发布到所需的目标页面并在该页面上处理服务器端帖子。

标签: jquery forms redirect submit


【解决方案1】:
$('#pgpost').submit(function(e) {  // this handles the submit event
    if($('#right-body-div').data('bValidator').validate()) {
        if ($("#honeypot").val() == "") {
            /*
             * url = $('#pgpost').val('action');
             * data = $('#pgpost').serialize();
             * $.post(url, data, function() {
             *     window.location = "http://www.homepage.co.uk/thankyou";
             * });
             */
            window.location = "http://www.homepage.co.uk/thankyou";
        }
        else {
            window.location = "http://www.homepage.co.uk/nothankyou";
        }
    }

    e.preventDefault();
    return false;
    /*
     * the 2 lines above are meant to prevent the normal behaviour
     * that the <form> would have (which is to submit via browser).
     * 
     * e.preventDefault() is the jquery way prevent the normal behaviour
     * return false is the legacy way to do it, you can use either or both
     * 
     * the reason for which the code didn't validate your form is
     * that e.prevenDefault() was only called inside the IF statement
     * which means that it would only be called if the field was
     * already in valid form. So basically if the form was invalid the normal
     * behaviour is not prevented and the browser submits the form, if the form
     * was valid the submit behaviour was prevented and the redirect was used.
     * 
     */
});

$("#submitme").click(function(){
    $('#pgpost').submit(); // this triggers the submit event
}); 

通过 JavaScript 处理提交事件时,您可以:

  • 总是阻止正常行为(我们的例子):
    • 当表单有效时,您通过 JavaScript submit the data,也许您在提交之前对其进行处理;
    • 当表单无效时,您会显示错误消息;
  • 仅在表单无效时阻止正常行为:
    • 当表单有效时,您允许正常行为(浏览器提交数据);
    • 当表单无效时,您会显示错误消息;

您的错误消息应说明蜜罐不应为空。

在以下情况下防止正常行为是有意义的: - 您需要在将数据发送到服务器之前对其进行处理(创建新值、更改当前值;不是验证); - 您需要避免页面刷新,因此您通过 AJAX 发送;

仅仅为了验证,阻止该行为是没有意义的,因为您可以验证然后允许正常行为继续。

【讨论】:

  • 感谢大家的快速回复,按照 Mihai 的代码,删除“return true”并在第二个 window.location 上添加“else”,现在表单提交和重定向,但是表单现在发布每个时间,即使字段无效 - 在我的原始代码中,蜜罐/提交只会在字段有效(嵌套)时运行。有什么进一步的建议吗? (大卫感谢您的评论,我会考虑您的建议,因为必须学习如何编写服务器端处理和回发等)。
  • 感谢 Mihai 花时间重新编写代码和其他 cmets...蜜罐/验证现在可以正常工作并阻止表单提交,重定向也正常工作,将用户发送到正确的页面取决于蜜罐值,但是表单似乎没有提交(处理表单并通过电子邮件发送到我的收件箱的 PHP 脚本没有任何改变)所以只能假设它与 .submit()... ..有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2015-11-04
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多