繁体   English   中英

从数据库中的数据填充下拉列表

[英]populating a dropdown from data in the database

我有以下。 HTML

 <div class="frmInput">
                        <select id="registrationTypeDropDown" class="select">
                            
                        </select>
                    </div>

Javascript

fetch('../svc/RegistrationForm.asmx/GetRegistrationType', {
    method: 'GET',
    credentials: 'same-origin',
    headers: {
        'Content-Type': 'application/json'
    }
}).then((response) => {
    types = response.json();
    
    for (const [ID, Name] of Object.entries(types)) {
        console.log(ID, Name);
        options += '<option value="' + ID + '" text="' + Name + '" />';
    }
    $('#registrationTypeDropDown').append(options);
  });

我尝试了不同的方法来获得我的结果,但没有任何效果。 当我运行它时,它甚至没有命中我的 for 循环。 如果有人能指出更好的方法或我做错了什么,我将不胜感激。

在此处输入图像描述

您实际上是将Promise分配给types变量试试这个。 另请参阅使用 fetch api

 fetch('../svc/RegistrationForm.asmx/GetRegistrationType', { method: 'GET', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' } }).then(response => response.json()). // you are missing this line then((response) => { types = response; for (const [ID, Name] of Object.entries(types)) { console.log(ID, Name); options += '<option value="' + ID + '" text="' + Name + '" />'; } $('#registrationTypeDropDown').append(options); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM