简体   繁体   English

GAS按钮或输入[提交]不断刷新页面? 为什么?

[英]GAS button or input[submit] keeps refreshing page? why?

I've just started a new Google app script and created a simple form with some jQuery behind it to handle the validation, shown below.我刚刚启动了一个新的 Google 应用程序脚本,并创建了一个简单的表单,背后有一些 jQuery 来处理验证,如下所示。

But whenever I click on the <button> I created, it just refreshes the page.但是每当我点击我创建的<button> ,它只会刷新页面。 The same thing happens with a <input type='submit'> button. <input type='submit'>按钮也会发生同样的事情。

Any ideas why this is doing his and how to prevent it?任何想法为什么这样做他以及如何防止它?

 <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> <!-- https://developers.google.com/apps-script/add-ons/css --> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> <!-- this CSS package is for the jquery datepicker --> <div> <h1>Use the form below to enter a new entry into the calendar</h1> <table> <form> <tr> <td> Title: <input type='input' id='title' /> </td> <td> Description:<input type='input' id='description' /> </td> </tr> <tr> <td> Date: <input type='text' id='date_choice' value="" /> </td> <td> <label for="select">Time of day</label> <select id="select"> <option value='before' >Before School Starts</option> <option value='morning' selected>Morning</option> <option value='lunchtime'>Lunchtime / Afternoon</option> <option value='afterschool'>After School has ended</option> </select> </td> </tr> <tr> <td colspan='2' style='text-align: center;'> <button class="create">Insert</button> </td> </tr> </form> </table> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script> <script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script> <script> $(document).ready(function(){ $( "#date_choice" ).datepicker({ dateFormat: "dd/mm/yy" }); $("#create").click(InsertIntoCal); }); function InsertIntoCal(){ var title = document.getElementById("title").value; var descr = document.getElementById("description").value; var date = document.getElementById("date_choice").value; var time = document.getElementById("select").value; alert(title); alert(descr); alert(date); alert(time); } </script>

You've made a simple HTML / jQuery mistake.您犯了一个简单的 HTML/jQuery 错误。 Your button has a class , but no unique id :您的按钮有一个class ,但没有唯一的id

<button class="create">Insert</button>
        ^^^^^^^^^^^^^^

...yet you attempt to bind a click() handler to a specific element by id : ...但您尝试通过idclick()处理程序绑定到特定元素:

$("#create").click(InsertIntoCal);
   ^^^^^^^

As a result, InsertIntoCal() never gets called.因此,永远不会调用InsertIntoCal()

You want that class for styling, so keep it but add an id like so:您需要class进行样式设置,因此保留它,但添加一个id如下所示:

<button class="create" id="insert-entry">Insert</button>

Then modify the binding to use the new id :然后修改绑定以使用新的id

$("#insert-entry").click(InsertIntoCal);

You can try it out in the snippet below.您可以在下面的代码段中尝试一下。

WRT <input type='submit'> - that will not work in Google Apps Script, there's no POST handler waiting for form submissions. WRT <input type='submit'> - 这在 Google Apps Script 中不起作用,没有等待表单提交的POST处理程序。 Instead, you need to keep extending your InsertIntoCal() handler to use google.script.run to send the form contents to a server-side handler.相反,您需要继续扩展您的InsertIntoCal()处理程序以使用google.script.run将表单内容发送到服务器端处理程序。

 <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> <!-- https://developers.google.com/apps-script/add-ons/css --> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"> <!-- this CSS package is for the jquery datepicker --> <div> <h1>Use the form below to enter a new entry into the calendar</h1> <table> <form> <tr> <td> Title: <input type='input' id='title' /> </td> <td> Description:<input type='input' id='description' /> </td> </tr> <tr> <td> Date: <input type='text' id='date_choice' value="" /> </td> <td> <label for="select">Time of day</label> <select id="select"> <option value='before' >Before School Starts</option> <option value='morning' selected>Morning</option> <option value='lunchtime'>Lunchtime / Afternoon</option> <option value='afterschool'>After School has ended</option> </select> </td> </tr> <tr> <td colspan='2' style='text-align: center;'> <button class="create" id="insert-entry">Insert</button> </td> </tr> </form> </table> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script> <script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script> <script> $(document).ready(function(){ $( "#date_choice" ).datepicker({ dateFormat: "dd/mm/yy" }); $("#insert-entry").click(InsertIntoCal); }); function InsertIntoCal(){ var title = document.getElementById("title").value; var descr = document.getElementById("description").value; var date = document.getElementById("date_choice").value; var time = document.getElementById("select").value; alert(title); alert(descr); alert(date); alert(time); } </script>

为了刷新,尝试在表单标签之外获取按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM