从 html onclick 事件调用 javascript 文件中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16448591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:12:55  来源:igfitidea点击:

Call function in javascript file from html onclick event

javascripthtmlfunctiongoogle-apps-script

提问by user1916075

This has been driving me crazy- I can't figure out why it wont work!

这让我发疯了 - 我不明白为什么它不起作用!

I have two files: myPage.html and myCode.gs in google scripts. I have deployed the html file as a web app, and I want the onclick event for the submit button to trigger the emailTech function from the myCode.gs file but it won't work! When I run the function straight from the file, it works fine.

我有两个文件:谷歌脚本中的 myPage.html 和 myCode.gs。我已将 html 文件部署为 Web 应用程序,并且我希望提交按钮的 onclick 事件触发 myCode.gs 文件中的 emailTech 函数,但它不起作用!当我直接从文件运行该函数时,它工作正常。

I've done a few hours of research and tried to add <script type="text/javascript" src="myCode.gs"></script>but that causes an error when I refresh the web app. I have tried calling the function in the onClick event as onClick= "google.script.run.emailTech()"and onClick= "emailTech()"but neither work. I have also tried loading the emailTech function into the script tag in the header, but that didn't work either! What am I missing? Please help!

我已经进行了几个小时的研究并尝试添加,<script type="text/javascript" src="myCode.gs"></script>但是当我刷新 Web 应用程序时会导致错误。我试过在 onClick 事件中调用该函数onClick= "google.script.run.emailTech()"onClick= "emailTech()"但都不起作用。我还尝试将 emailTech 函数加载到标题中的脚本标记中,但这也不起作用!我错过了什么?请帮忙!

myPage.html file:

myPage.html 文件:

    <script type="text/javascript"></script>
    <body>
    <input type="submit" onclick="emailTech();" value="Submit" />
    </body>

myCode.gs file:

myCode.gs 文件:

    function doGet() {
  return HtmlService.createHtmlOutputFromFile('myPage');
}

function emailTech(){

  Logger.log("is this firing?");
  var message = "This is the email message";
  MailApp.sendEmail("[email protected]", "This is the subject", message );

}

采纳答案by Mogsdad

EDIT: New answer - use google.script.run.

编辑:新答案 - 使用google.script.run.

<script type="text/javascript"></script>
<body>
<input type="button" onclick="google.script.run.emailTech();" value="Submit" />
</body>

回答by Corey G

You were actually on track with this:

你实际上是这样做的:

<script type="text/javascript"></script>
<body>
<input type="button" onclick="emailTech();" value="Submit" />
</body>

Don't use a submit; use a button. The semantics of submits and onclick handlers are a little bizarre (not just because of HtmlService sandboxing, but even in general) and don't play well with google.script.run. This is documented in the HtmlService user guide:

不要使用submit; 使用button. submits 和 onclick 处理程序的语义有点奇怪(不仅仅是因为 HtmlService 沙盒,而且在一般情况下也是如此)并且不能很好地与 google.script.run 配合使用。这记录在 HtmlService 用户指南中:

" You cannot use this technique with a regular submit button"

“您不能将此技术与常规提交按钮一起使用”