gpt4 book ai didi

javascript - 使用 SQLite 字段的内容创建 html 下拉列表

转载 作者:行者123 更新时间:2023-11-28 06:28:16 24 4
gpt4 key购买 nike

我正在尝试使用嵌入式 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>

而且它仍然不起作用。还有更多建议吗?

最佳答案

您无法关闭您的<script>标记在循环的中间。相反,您应该创建 <option> javascript 中的元素,然后将其附加到您的 <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);
}

关于javascript - 使用 SQLite 字段的内容创建 html 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34929196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com