[英]Populate drop-down using Json data that is from database(using Python FLASK)
I have JSON data which I retrieved from a PostgreSQL database into Flask function that returns the following:我有从 PostgreSQL 数据库检索到的 Flask 函数的 JSON 数据,该函数返回以下内容:
Flask function:烧瓶功能:
def Install():
cur = conn.cursor()
# cur.execute("""create table Install (Install varchar(300));""")
# cur.execute("""insert into Install (Install) values ('ffr2nightly.tst');""")
cur.execute("""select * from Install;""")
conn.commit()
test = {}
count = 0
for row in cur.fetchall():
test[count] = row[0]
count += 1
return test
Output:输出:
{
"0": "ffr2nightly.tst",
"1": "ffr2nightly.tst",
"2": "HealthCheckDocker.tst",
"3": "NoInstall.tst"
}
I am trying to use this data in my HTML form with dropdown list我正在尝试在带有下拉列表的 HTML 表单中使用此数据
HTML: HTML:
<select multiple="multiple" id='lstBox3' class="col-md-5">
<option>
// I want the Json data in these options
</option>
</select>
Yeah, you just need to extract the values from the JSON, then parse it into the select element using jQuery.是的,您只需要从 JSON 中提取值,然后使用 jQuery 将其解析为 select 元素。
//Your data var data = { "0": "ffr2nightly.tst", "1": "ffr2nightly.tst", "2": "HealthCheckDocker.tst", "3": "NoInstall.tst" }; //iterate through it and append to the select Object.values(data).forEach(function(ele) { var opt = '<option>' + ele + '</option>'; $('#sel').append(opt); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="sel"></select>
If you want to exclude duplicates, you just check if the option exists before adding it with this如果要排除重复项,只需在添加此选项之前检查该选项是否存在
if ($('#sel').find('option[value="' + ele + '"]').length == 0)
$('#sel').append(opt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.