我正在尝试动态添加选项,就像此处给出的示例一样:https://github.com/google/google-apps-script-samples/tree/master/simple_tasks
我使用几乎完全相同的代码,但每次都会收到错误:
Uncaught TypeError: Cannot set property length of [object Object] which has only a getter
我的代码.gs:
function doGet(e) {
return HtmlService.createTemplateFromFile('index').evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE).setTitle('RepairAdminSysteem');
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function data() {
var ss = SpreadsheetApp.openById(SS_ID).getActiveSheet();
var data = ss.getDataRange().getValues();
Logger.log(Utilities.jsonStringify(data));
}
function init() {
var status = ['VERSTUURD','NIET VERSTUURD','AKKOORD','NIET AKKOORD','HERRINNERING','TWEEDE HERRINNERING','GEEN REACTIE','NIET VAN TOEPASSING','PAUZE'].sort();
return status;
}
我的(相关)html:
<select id='status' disabled>
<option>Loading...</option>
</select>
和 JavaScript:
<script>
$(function() {
$('#status').bind('change', loadPO);
start();
});
function start() {
google.script.run.withSuccessHandler(success)
.init();
}
function success(status) {
var select = $('#status');
select.empty();
status.forEach(function(elem) {
var option = $('<option>').attr('value',elem).text(elem);
select.append(option);
});
select.prop('disabled', false);
loadPO();
}
function loadPO() {
alert('test');
}
</script>
如果我将 select.empty();
更改为 select.html('');
一切都会按预期工作,但我不明白为什么会这样在简单任务示例中工作,而不是在我的代码中工作。
我想知道的是,为什么这个几乎相同的代码在上面链接中的简单任务应用程序中运行,而不是在我的应用程序中运行?
最佳答案
function success(status) {
var select = $('#status');
var option = '';
status.forEach(function(elem) {
option = option '<option value="'+elem+'">'+elem+'</option>';
});
select.html(option); // here html reset by new option
select.prop('disabled', false);
loadPO();
}
引用 html()
关于jquery - 无法在 select 上的 .empty() 之后使用 .append,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427527/