【发布时间】:2015-03-02 02:25:10
【问题描述】:
我正在尝试根据来自另一个列表框的选择来填充列表框。它在 Apache 服务器上加载的简单 html/js 页面上运行良好,但是当我尝试将其放入 Google App Script 中的 html 服务时,只出现第一个列表框:第二个框中没有任何反应。我觉得我错过了 GAS 中的一些基本概念。
在 code.gs 我有:
function hardCode() {
var html = HtmlService.createHtmlOutputFromFile('hardcode.html')
.setWidth(600).setHeight(425);
SpreadsheetApp.getUi().showModalDialog(html, 'Why doesn't this work');
}
然后在html方面我有:
<form name="classic">
<select name="countries" size="4" onChange="updatecities(this.selectedIndex)" style="width: 150px">
<option selected>Select A City</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
<select name="cities" size="4" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
</select>
</form>
<script type="text/javascript">
var countrieslist=document.classic.countries
var citieslist=document.classic.cities
var cities=new Array()
cities[0]=""
cities[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
cities[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
cities[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue",
"Birmingham|birminghamvalue"]
function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0],
cities[selectedcitygroup][i].split("|")[1])
}
}
</script>
【问题讨论】:
-
尝试将
.setSandboxMode(HtmlService.SandboxMode.IFRAME)添加到 HtmlService。 -
另一件事,当我进入 javascript 控制台时,我收到此错误:未捕获的类型错误:无法读取未定义的属性“国家”。
-
成功了!抱歉,我错误地插入了您的代码 sn-p。根据 GAS 开发者网站的说法,这种沙盒模式与旧版浏览器的向后兼容性较差,但提供了更多的 HTML5 约定。再次感谢您的帮助!