【问题标题】:Stop form from redirecting on submit阻止表单在提交时重定向
【发布时间】:2017-03-23 18:56:40
【问题描述】:

我很清楚这个问题已经被问过好几次了,但我已经尝试了至少 6 种解决方案,但都没有奏效。我正在收集数据以发送到谷歌表单,但在提交表单时,浏览器会重定向到成功页面。我希望这一切都使用 AJAX 发生,但我的代码不起作用。

HTML:

<form  id="userinfo" method="get" action="https://script.google.com/macros/s/xxx/exec" accept-charset="UTF-8" onsubmit="return false">
    <input type="text" name="name" id="formname" placeholder="Name">
    <input type="text" name="email" id="formemail" placeholder="Email">placeholder="Game Days">
    <input type="submit" value="submit" id="upload_data"/>
</form>

JS:

$("#userinfo").submit(function(e) {

        var urll = "https://script.google.com/macros/s/xxx/exec"; // the script where you handle the form input.

        $.ajax({
               type: "GET",
               url: urll,
               data: $("#userinfo").serialize(), // serializes the form's elements.
               success: function(data)
               {
                   alert(data); // show response from the php script.
               }
             });

        e.preventDefault(); // avoid to execute the actual submit of the form.
    });

【问题讨论】:

  • 首先,您应该阻止第一行的默认操作,这样如果发生错误而停止处理 javascript,则不会发生常规 sumbit,因为它已经被阻止了。

标签: javascript jquery ajax forms


【解决方案1】:

您可以使用jQuery Form Plugin 发送表单而无需提交。

你的代码应该是这样的:

$("#userInfo").ajaxSubmit({
   success: function(data)
   {
       alert(data); // show response from the php script.
   }
});

【讨论】:

    猜你喜欢
    • 2016-03-15
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多