Google Apps脚本-Javascript不起作用 [英] Google Apps Script - Javascript not working
本文介绍了Google Apps脚本-Javascript不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个简单的Web应用,但无法正常工作.
I created a simple web app, and it is not working.
Code.gs:
function doGet() {//I think this works
var output = HtmlService.createTemplateFromFile('index').evaluate();
output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
output.setTitle('email button');
return output;
}
function getContent(filename) {//might work
Logger.log('getContent()');
return HtmlService.createTemplateFromFile('javascriptCode').getRawContent();
}
index.html:
index.html:
<!DOCTYPE html>
<html>
<body>
<form id="email_subscribe">
<input type="email" name="email" id="email" placeholder="Enter your email">
<input type="button" id="submitButton" value="Submit">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
<?!= getContent("javascript") ?>
</body>
</html>
javascriptCode.html:
javascriptCode.html:
<script type="text/javascript">
$(document).ready(function() {
$("#submitButton").onclick(function() {
alert('thx for submitting');
});
});
</script>
当我运行此WebApp时,电子邮件和按钮字段将正确加载,并且记录器将记录"getContent()".但是,当我单击该按钮时,什么也没有发生.我在做什么错了?
When I run this webApp, the email and button fields load properly, and the logger logs 'getContent()'. However, nothing happens when I click the button. What am I doing wrong?
谢谢
推荐答案
您的代码中几乎没有错误.
There are quite few errors in your code.
- 脚本标签不是自动关闭标签.
- 提供HTTP版本的jquery(有时为http内容)更安全 将被阻止.
- jquery中有 NO 个onclick事件,它只是 click .
- Script tag is not a self closing tag.
- It is safer to serve https version of jquery, sometime http content will be blocked.
- There is NO onclick event in jquery it is just click.
code.gs
function doGet() {
var output = HtmlService.createTemplateFromFile('index').evaluate();
output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
output.setTitle('email button');
return output;
}
function getContent(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
index.html
<!DOCTYPE html>
<html>
<body>
<form id="email_subscribe">
<input type="email" name="email" id="email" placeholder="Enter your email">
<input type="button" id="submitButton" value="Submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<?!= getContent("javascriptCode") ?>
</body>
</html>
javascriptCode.html
<script type="text/javascript">
$(document).ready(function() {
$("#submitButton").click(function() {
alert('thx for submitting');
});
});
</script>
这篇关于Google Apps脚本-Javascript不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文