簡體   English   中英

從html onclick事件調用javascript文件中的函數

[英]Call function in javascript file from html onclick event

這讓我發瘋了 - 我無法弄清楚為什么它不會起作用!

我有兩個文件:google腳本中的myPage.html和myCode.gs。 我已將html文件部署為Web應用程序,我希望提交按鈕的onclick事件從myCode.gs文件中觸發emailTech函數,但它不起作用! 當我直接從文件中運行該函數時,它工作正常。

我已經做了幾個小時的研究並試圖添加<script type="text/javascript" src="myCode.gs"></script>但是當我刷新Web應用程序時會導致錯誤。 我已經嘗試在onClick事件中調用該函數,如onClick= "google.script.run.emailTech()"onClick= "emailTech()"但是都不起作用。 我也嘗試將emailTech函數加載到標題中的腳本標記中,但這也不起作用! 我錯過了什么? 請幫忙!

myPage.html文件:

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

myCode.gs文件:

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

function emailTech(){

  Logger.log("is this firing?");
  var message = "This is the email message";
  MailApp.sendEmail("XYZ@abc.com", "This is the subject", message );

}

你實際上是這樣的:

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

不要使用submit ; 使用button 提交和onclick處理程序的語義有點奇怪(不僅僅是因為HtmlService沙盒,但即使是一般)並且與google.script.run不能很好地兼容。 這在HtmlService用戶指南中有記錄:

“你不能在常規提交按鈕中使用這種技術”

編輯:新答案 - 使用google.script.run

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM