javascript - Google Script - 侧边栏按钮不断打开一个新标签

标签 javascript forms google-apps-script web-applications form-submit

我正在创建一个非常基本的脚本来帮助我处理我的一个谷歌电子表格。

我已经成功地显示了一个侧边栏,其中有几个按钮(哪个功能)。但是,每当我单击其中一个按钮时,它还会打开一个新选项卡,其 URL 类似于:“https://n-lx3mdv5ls3mdgglsq226llilxd2m4owxy72y3fy-1lu-script.googleusercontent.com/userCodeAppPanel ?”

即使按钮没有任何功能,这种情况仍然会发生。

这是 html 的示例:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <!-- The CSS package above applies Google styling to buttons and other elements. -->

    <style>
    
    </style>
  </head>
  <body>
    <div class="sidebar">
      <form>
       <div class="block" id="buttons">
          <button id="unindent">Unindent</button>
          <button id="indent">Indent</button>
          <button id="asdgdgasg">Test</button>
        </div>
      </form>
    </div>
  </body>
</html>

我怎样才能阻止这个新标签每次都打开?

最佳答案

问题:

  • <button>类型,如果未指定默认为 type=submit .所以,表单自动将数据提交给服务器。这在 iframe 中不可取。

解决方案:

  • 将按钮类型明确指定为按钮。 <button type='button' >
  • 使用event.preventDefault()防止自动提交表单。

引用资料:

关于javascript - Google Script - 侧边栏按钮不断打开一个新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825069/

相关文章:

google-apps-script - Google 应用电子表格父文件夹 ID

google-apps-script - Google 电子表格脚本 - 获取单个引用单元格的 getColumn

javascript - Jquery 移动按钮和 i18next 翻译

javascript - 动态添加 jQuery 事件

forms - ReCaptcha 无效 + CodeIgniter 表单助手 = 表单数据丢失

javascript - HTML 表单操作 - "Order of Operation"

javascript - 连接到 angular-ui-router $transitions API 时出现意外的 window.confirm 行为

javascript - 如何创建类似jquery的$()包装函数?

javascript - FormData() 对象在 Django 后端始终为空

google-apps-script - 使用 Google Advanced Drive 服务在带有 Apps Script 的文件夹中创建新文件