2017-04-27 167 views
0

我想将表单回复发送给Google工作表,然后将用户重定向到感谢页面。 Google脚本将用户直接重定向到报告页面。我想放弃谷歌重定向并将我的地方。使用ajax重定向表单提交

形式:

<form method="post" id="formID" action="gscript"> 
... 
</form> 

脚本:

<script> 
    $(document).on('submit', '.formID', function(e) { 
    $.ajax({ 
     url: $(this).attr('action'), 
     type: $(this).attr('method'), 
     data: $(this).serialize(), 
     success: function(html) { 
     alert('ok'); 
     window.location("Thanks page link"); 
     } 
    }); 
    e.preventDefault(); 

}); 
</script> 
+1

不应该'.formID'是'#formID'? – manelgarcia

+0

对,它应该x) – Kapparina

+0

仍然无法正常工作。 :( – David

回答

0

添加.success()回调函数,当请求完成后,好了,成功地被调用。在该功能中,将用户重定向到您所需的谢谢页面。

像这样:

所有的
$.ajax({ 
    url: $(this).attr('action'), 
    ... 
    success: function(result) { 
     // result unused, drop it if you want :) 
     window.location.replace('url_to_your_thank_you_page'); 
    } 
}); 
+0

他已经有一个 –

+0

哇我抄了他的代码,并没有成功的功能,我是否失明或做过这样的巨魔吗? – Kapparina

+0

太多的咖啡和时间在屏幕前,发生在我们身上:) –

0

首先改变这种$(document).on('submit', '.formID', function(e) {这个$(document).on('submit', '#formID', function(e) {
现在试着放置在上面这条线e.preventDefault();,在ajax调用之前:

$('#formID').submit(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
     url: $(this).attr('action'), 
     type: $(this).attr('method'), 
     data: $(this).serialize(), 
     success: function(html) { 
      alert('ok'); 
      window.location("Thanks page link"); 
     } 
    }); 
}); 

编辑
您的评论下面,我不熟悉版gscript的API,但也许考虑将button,将调用你的功能有问题吗$('#myForm').submit()

+0

不起作用,但谢谢。 :) – David

+0

我已经更新我的答案尝试'$('#formID')。submit'。什么不起作用,你会得到一个错误? –

+0

当您使用google脚本作为操作时,提交按钮会将您发送到报告页面。这就像剧本不重要,仍然没有错误。 – David