【发布时间】:2017-03-20 17:22:10
【问题描述】:
所以目前我有一个下拉菜单,其中填充了一个 json 文件,每个选项看起来都像这样(实际文件中有更多集合):
JSON
{
"thing": {
"Selected thing": [
{
"name" : "thing 1",
"value" : 1.0,
"purpose": "thing 1"
},
]
}
}
它们正在使用脚本填充到下面的 HTML 选择中:
HTML
<tr>
<td><strong>Choose a Thing</strong></td>
<td>
<div>
<select name="thing" class="form-control" id="thing" value="{{ thing }}">
<option class ="placeholder">{{ thing }}</option>
</select>
</div>
</td>
</tr>
JavaScript
$(document).ready(function () {
var menu = document.getElementById('thing')
$.getJSON("{{ url_for('static', filename='things.json') }}", function (data) {
$.each(data.things, function (optgroup, options) {
var next_optgroup = document.createElement('OPTGROUP')
next_optgroup.setAttribute("label", optgroup)
menu.appendChild(next_optgroup)
$.each(options, function (index) {
next_option = document.createElement('OPTION')
next_option.setAttribute("value", options[index].value)
next_option.setAttribute("name", options[index].name)
next_option.innerHTML = options[index].name
next_optgroup.appendChild(next_option)
});
});
});
})
因此,目前,一旦选择了选定的选项并按下了提交按钮,值 (1) 就会传递给 Flask 并通过 {{ thing }} 存储并呈现给选择选项值。
我需要它正常工作,但我还需要能够存储与所选选项关联的名称,我觉得我可能需要使用 javascript 或一些隐藏的输入(肯定在我的HTML 表单)来做到这一点,尽管我对 javascript 的了解很少。
换句话说,我如何能够存储并在刷新页面时保持选定的选项名称(事物 1)显示在 HTML 页面上?
【问题讨论】:
-
将changeListener 附加到选择并将更改保存到localStorage
标签: javascript jquery html json flask