我试图让jquery 自动完成功能在 HtmlService 模板中工作。当我输入它时,函数内部生成的数组按预期工作。这个想法是提供来自 ScriptDb 数据库的数据,我编写了一个函数,该函数从数据库返回一个数组(按预期返回一个数组):
function doGet(e) {
var htmlTemplate = HtmlService.createTemplateFromFile('Search');
htmlTemplate.diseases = ['aname','bname','cname','dname','ename','fname','gname'];
Logger.log(htmlTemplate.diseases instanceof Array);
return htmlTemplate.evaluate();
}
在这种情况下,Logger 返回 true。
<!DOCTYPE html>
<html>
<head>
<!-- <title>The Disease Database</title> -->
<meta name="author" content="M. Oranje" />
<meta name="description" content="My personal disease database" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/smoothness/jquery-ui.css" rel="Stylesheet" />
</head>
<body>
<form action="https://script.google.com/macros/s/AKfycbxHmY6ejv8SnmnOupKKrieImaPTbCZAdtuQl_n8z78/dev" method="POST" id="diseases-form">
<div id="page">
<div class="search">
<div class="name" name="name" id="name">Search</div>
<div class="search_input"><input name="search" id="search" type="text"></div>
</div>
</div>
</form>
<script>
$(function() {
var tags = <?= diseases ?>;
alert(tags instanceof Array);
$( "#search" ).autocomplete({
source: tags
});
});
</script>
</body>
</html>
然而,这里的警报返回 false,并且强制打印数组返回引用错误('aname is not defined')。