如何使用java脚本以下拉形式显示json数据?

2024-04-19 18:32:49 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码。 这里我返回一个json数据对象。你知道吗

def db_dropdown():  # Execute query
    db_path = "C:\sqlite\marketoperation.db"
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    c.execute('''SELECT id,name FROM Generator''')
    row_headers = [x[0] for x in c.description]  # this will extract row headers
    rv = c.fetchall()
    print(rv)
    json_data = []
    for result in rv:
        json_data.append(dict(zip(row_headers, result)))
    print('executed')
    print(json.dumps(json_data))
    data=json.dumps(json_data)
    return data

“数据”的格式如下: [{“id”:1,“name”:“a1”},{“id”:2,“name”:“a2”}] 这是我的电话号码试用.html传递json数据。你知道吗

@app.route('/trying', methods=['POST', 'GET'])
def index1():
    list_tested = db_dropdown()
    print(list_tested)
    return render_template("try.html", trial1=list_tested)

这是我的试用.html你知道吗

<body>
        <p>Hello World</p>
        <select id="gendropdown" name="gendropdown">
        </select>
        <button type="submit" onclick="clicked()">Submit</button>
    <script>
 var trail1 = {{trail1}}
          var jobj = JSON.parse(trail1);
          let dropdown = document.getElementById('gendropdown');
           dropdown.length = 0;

            let defaultOption = document.createElement('option');
            defaultOption.text = 'Choose State/Province';

            dropdown.add(defaultOption);
            dropdown.selectedIndex = 0;
            let option;
    for (let i = 0; i < jobj.length; i++) {
      option = document.createElement('option');
      option.text = data[i].name;
        option.value = data[i].id;
        dropdown.add(option);

        </script>
      </body>

我试图以下拉列表的形式发布json数据。但它不起作用。 请建议更改。我想以下拉格式显示json数据。你知道吗


Tags: 数据nameidjsonfordbdatahtml