【发布时间】: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