【问题标题】:How prevent form submit in google apps script如何防止在谷歌应用脚​​本中提交表单
【发布时间】:2021-07-14 12:07:12
【问题描述】:

我正在将用 GoogleApps 脚本编写的表单导入到使用 Squarespace 构建的页面上的 iframe 中,但我终生无法阻止表单提交。我正在使用:

window.addEventListener( 'load', preventFormSubmit );

按照 GAS 文档中的建议,但这似乎不会触发 preventFormSubmit 函数。相反,当单击提交按钮时,表单提交并转到一个空白的谷歌页面。此外,preventFormSubmit 函数中的警报(现在已被注释掉)永远不会显示,这表明该表单从未被调用。

我在这上面花了几天时间,在任何地方都找不到答案,也不再只见树木不见森林。关于我做错了什么的任何线索?

Squarespace 是一种网站构建器,可以嵌入代码,在本例中为 iframe。

我的代码: js.html:

    <script>

   function preventFormSubmit() {
    //alert( "prevent form submit triggered" );
    var forms = document.querySelectorAll('form');
    for (var i = 0; i < forms.length; i++) {
      forms[i].addEventListener('submit', function(event) {
        event.preventDefault();
        
      });
    }
    //alert( "forms prevented from submitting: " = forms.length );
  }

  window.addEventListener( "ready", preventFormSubmit );
  
  function handleFormSubmit(formObject) {
    google.script.run
      .withSuccessHandler( showSuccess )
      .withFailureHandler( showError )
      .processForm_1( formObject );
  }
   </script>    

html:

<!DOCTYPE html >
<head>
  <base target="_top" >
  
  
  <?!= include('css'); ?>
  <?!= include('js'); ?>

</head>
<body>

  <div id="formDiv" class="card" >
    <h2 id="title" >Alternative Booking form</h2>
    
    <form id="alternative-booking-form-1" onsubmit="handleFormSubmit(this)" >
      <fieldset>
        <legend>About You</legend>
        <p>Please tell us a bit about yourself. 
        </p>
        <input type="text" id="firstName" name="firstName" form="alternative-booking-form-1" 
                                                  placeholder="your first name" value="" required 
    /><br />
        <input type="text" id="lastName" name="lastName" form="alternative-booking-form-1" 
                                                  placeholder="your last name" value="" required 
    /><br />
        <input type="text" id="title" name="title" form="alternative-booking-form-1" 
                                    placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>  
                                                                   
      </fieldset>
      <fieldset>
        <legend>Your Contact Details</legend>
        <p>We will only use your contact details in case we need to contact you with regard to 
      this booking, unless you consent
          to further communications, as offered later in this booking process.</p>
        <input type="email" id="email" name="email" form="alternative-booking-form-1" 
                                                  placeholder="your email address" value="" 
    required /><br />
        <input type="tel" id="phone" name="phone" form="alternative-booking-form-1" 
                                                  placeholder="phone" value="" required /><br />                                          
      </fieldset> 
                                                                                                    
      <fieldset>
        <input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1"  />
        <br />
        <input type="submit" id="submit" name="submit" form="alternative-booking-form-1" 
     class="red" value="Next &rarr;"  /> 
        <br /> 
        <br />
      </fieldset>
    </form>
  </div>
  <div id="output" name="output" ></div>
  
    </body>

【问题讨论】:

    标签: forms google-apps-script iframe


    【解决方案1】:
    <!DOCTYPE html >
    <head>
      <base target="_top" >
      
      
      <?!= include('css'); ?>
      <?!= include('js'); ?>
    
    </head>
    <body>
    
      <div id="formDiv" class="card" >
        <h2 id="title" >Alternative Booking form</h2>
        
        <form id="alternative-booking-form-1" >
          <fieldset>
            <legend>About You</legend>
            <p>Please tell us a bit about yourself. 
            </p>
            <input type="text" id="firstName" name="firstName" form="alternative-booking-form-1" 
                                                      placeholder="your first name" value="" required 
        /><br />
            <input type="text" id="lastName" name="lastName" form="alternative-booking-form-1" 
                                                      placeholder="your last name" value="" required 
        /><br />
            <input type="text" id="title" name="title" form="alternative-booking-form-1" 
                                        placeholder="your title, eg: mr, mrs, ms etc" value="" /><br>  
                                                                       
          </fieldset>
          <fieldset>
            <legend>Your Contact Details</legend>
            <p>We will only use your contact details in case we need to contact you with regard to 
          this booking, unless you consent
              to further communications, as offered later in this booking process.</p>
            <input type="email" id="email" name="email" form="alternative-booking-form-1" 
                                                      placeholder="your email address" value="" 
        required /><br />
            <input type="tel" id="phone" name="phone" form="alternative-booking-form-1" 
                                                      placeholder="phone" value="" required /><br />                                          
          </fieldset> 
                                                                                                        
          <fieldset>
            <input type="hidden" id="form" name="form" form="alternative-booking-form-1" value="1"  />
            <br />
            <input type="button" id="submit" name="submit" form="alternative-booking-form-1" 
         class="red" value="Next &rarr;"  /> 
            <br /> 
            <br />
          </fieldset>
        </form>
      </div>
      <div id="output" name="output" ></div>
      <script>
      window.onload = function() {
        document.getElementById("alternative-booking-form-1").addEventListener( "ready", handleFormSubmit );
      }
       
      function handleFormSubmit(formObject) {
        google.script.run
          .withSuccessHandler( showSuccess )
          .withFailureHandler( showError )
          .processForm_1( formObject );
      }
       </script>    
        </body>
    

    【讨论】:

    • 感谢您的回复,但它仍然会发布表单,我想阻止这种情况。问题是默认行为发生在 handleFormSubmit() 有时间运行之前 - 即使我将事件侦听器中的 handleFormSubmit() 替换为 preventDefault
    • 我不明白它怎么可能。它不是 submit 类型,也没有 submit 属性。
    • 不过,我想知道你的想法。将事件侦听器从加载到文档头中移动到正文几乎完全加载后会有什么影响?
    • 因为脚本在所有内容加载完毕后才需要运行
    • 好的,我想我现在已经正确实现了。表单不再提交但不会触发 handleFormsubmit() 函数。 window.onload 语句是什么意思?在我看来,当 html 文档完全加载时,获取表单并等待它准备好,然后调用 handleFormSubmit()。它是否正确?如果是这样,表格什么时候准备好?加载时,还是单击按钮时?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多