[英]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.