创建包含 SQLite 字段内容的 html 下拉列表
Creating a html dropdown list with the contents of a SQLite field
我正在尝试使用嵌入式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>
标签。相反,您应该在 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);
}
相关文章:
- 如何在 HTML 下拉列表中制作锚标记
- 数据和HTML下拉列表
- HTML 下拉列表所选值
- 如何根据HTML下拉列表中的数量选择来计算价格
- 从 html.append 追加的 html 下拉列表中获取返回值
- 在复选框取消选中事件中从HTML下拉列表中删除项目
- HTML 下拉列表无法捕获 onChange() 如果下拉列表中只有一个值使用 javascript
- HTML 下拉列表到 Javascript 变量显示错误
- 如何从HTML下拉列表中选择多个值,而无需按住Ctrl按钮
- JavaScript 创建带有日期的 HTML 下拉列表
- 根据在 html 下拉列表中选择的国家/地区填写当前时间
- 在 POST MVC 之后保存 Html 下拉列表值
- 如何根据查询返回选择 html 下拉列表
- HTML下拉列表将我带到错误的URL
- JQuery 根据 HTML 下拉列表选择查找并替换 URL 中的查询字符串
- 使用 AJAX 从 databse 填充 HTML 下拉列表
- 使用 jQuery 和 html 下拉列表加载页面
- 创建包含 SQLite 字段内容的 html 下拉列表
- 在 html 下拉列表中选择默认值
- 从具有不同值的逗号分隔JSON对象填充html下拉列表