【问题标题】:Google spreadsheet website form jQuery script block the sending ofGoogle 电子表格网站表单 jQuery 脚本阻止发送
【发布时间】:2014-12-16 16:12:40
【问题描述】:

我的表单是 html 格式的。我从原始 Google 表单中复制了操作、输入键和隐藏输入。

<form id="Gform" role="form" action="https://docs.google.com/forms/d/MY-FORM-KEY/formResponse" method="POST" target="_self" onsubmit="">
                <input type="text" pattern="^[a-zA-Z][a-zA-ZążźćśęółbńĄŻŹĆŃŚĘÓŁ ]{4,40}$" name="entry.1028663680" value="" id="entry.1028663680" dir="auto" title="What is Your city" class="form-control" placeholder="City" required>
                <input type="text" pattern="^[a-zA-Z][a-zA-ZążźćśęółbńĄŻŹĆŃŚĘÓŁ ]{5,40}$" name="entry.1220908348" value="" id="entry.1220908348" dir="auto" title="Your complete name" class="form-control" placeholder="Your name" required>
                <input type="tel" name="entry.623688995" value="" id="entry.623688995" dir="auto" title="" class="form-control" placeholder="phone" required>
                <input type="email" name="entry.1564973803" value="" id="entry.1564973803" dir="auto" title="" class="form-control" placeholder="email" required>
                <input type="hidden" name="entry.383122401" value="WEBSITE" id="entry.383122401" dir="auto" title="" class="form-control" placeholder="E-mail" required>
                <input type="hidden" name="draftResponse" value="[,,&quot;-9139933475238999509&quot;]
">
<input type="hidden" name="pageHistory" value="0">
<input type="hidden" name="fbzx" value="-9139933475238999509">
                <button type="submit" name="submit" id="sendData" class="btn btn-default">Submit</button>
            </form>

我有一个 jQuery 脚本,它显示确认:

    <script type="text/javascript">
    function sendContactForm(){
                $("#Gform").fadeOut();
                $("#form-container").append('<p class="confirmer">Thanks!<br>Your email was sent.</p>');
           };
$('#Gform').submit(function () {
 sendContactForm();
 return false;
});
</script>

当我删除此脚本时,表单正在发送,并保存到谷歌,但点击提交后,我重定向到谷歌“谢谢”页面。我不想重定向并显示确认

就像在脚本中一样。如何解决这个问题?

【问题讨论】:

  • 您正在将MY-FORM-KEY 更改为您的谷歌密钥,对吧?你得到什么错误?您的表格是否已提交? (在 Google Chrome 上按 F12 并观看 网络 部分)。
  • 已提交,但看起来所有输入的值都是空的,但它们不是空的
  • 您是否检查了表单中每个 input 的正确 name="..." 以及 Google 表单中的预期名称?
  • 是的,所有输入的名称和ID都是正确的
  • @rodrigogq 我找到了显示确认提交表单的 javascript。该脚本如下所示: 如果我评论这个脚本,表单被发送,如何修复它?

标签: javascript jquery html google-sheets google-forms


【解决方案1】:

尝试使用 AJAX 来完成你的任务,当你使用异步功能时,不会有重新加载页面,你会在后台发送数据。在数据对象中输入所有输入值,在 done() 和 fail() 函数中定义收到响应时要做什么。祝你好运:)

$('#formID').submit(function(e){
   e.preventDefault();
   $.ajax({
      url: "https://docs.google.com/forms/d/1PrgHQALlz0WrvwjhGpLrtIgD5aQ1x-8HrOubkxTLNKs/formResponse",
      type: "POST",
      data: {
         'entry.111':     $('#entry_111').val(),
         'entry.222': $('#entry_222').val(),
        // all data from form
      }
   }).done(function(data){
      yourAction(data);
   }).fail(function(data){
      failAction(data);
   });
});

【讨论】:

  • 表单已发送,但未显示带有确认的元素

  • 如果你想说 .done() 上的操作没有执行,那么你是对的,因为在从这个请求中接收数据后,你会收到包含错误(重定向问题),就是这样将您的操作放入 fail() 函数中。不用担心,您的表单将被发送,请查看。
  • 好的,现在可以使用了!是啊 :D 非常感谢。一切都很好:)
【解决方案2】:

在上面的代码中注释return false

$('#Gform').submit(function () {
    sendContactForm();
    //return false;
});

或者只是删除这一行。 这个脚本正在做的是:订阅submit 事件并取消它。

你想做点别的吗?

【讨论】:

  • 我只想发送表单而不重定向到谷歌表单确认页面,并在联系表单 div 中显示“谢谢”。谢谢,我会删除这一行
  • 表单已发送,但也重定向到谷歌页面。如何停止重定向?
  • 将表单标签 target='self' 更改为 target='_blank' 或其他任何地方。无论如何,你不会摆脱打开另一个窗口或重定向。但它不适用于较旧的 HTML。我无能为力,抱歉。 w3schools.com/tags/att_form_target.asps
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 2020-04-14
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2022-11-16
相关资源
最近更新 更多