我正在尝试使用嵌入式JavaScript将SQLite字段作为html下拉列表的内容。这是我的代码,但它不起作用。
<select>
<option value="default">Select a test to edit</option>
<script>
var db = openDatabase('ExamSQL', '1.0', 'database')
db.transaction(function (tx) {
tx.executeSql('SELECT Questions FROM Question',[],function(tx,questions);
{
var len = questions.rows.length, i;
for (i = 0; i < len; i++) {
</script><option value="name"><script>
document.write(questions.rows.item(i).text)
document.getElementById("name").innerHTML=questions.rows.item(i).text
</script></option>
<script>
}
});
</script>
</select>
有人可以帮忙吗?
编辑:我在BrokenBinary的评论后做了这个
<select id="section_list">
<option value="default">Select a section the question is in</option>
<script>
var db = openDatabase('ExamSQL', '1.0', 'database')
db.transaction(function (tx) {
tx.executeSql('SELECT SectionName FROM Section',[],function(tx,sections);
{
var select=document.getElementById('section_list')
var len = sections.rows.length, i;
for (i = 0; i < len; i++) {
var option = document.createElement('option');
option.text=sections.rows.item(i).text;
option.value=sections.rows.item(i).text;
select.appendChild(option);
}
}
});
</script>
</select>
而且它不起作用。还有其他建议吗?
答案 0 :(得分:0)
您无法在循环中间关闭<script>
标记。相反,您应该在javascript中创建<option>
元素,然后将其附加到<select>
元素。
所以你的循环看起来像这样:
// Give your select an ID and use it here
var select = document.getElementById('my-select');
var len = questions.rows.length, i;
for (i = 0; i < len; i++) {
var option = document.createElement('option');
option.text = questions.rows.item(i).text;
option.value = questions.rows.item(i).text;
select.appendChild(option);
}