【发布时间】:2021-09-25 12:57:17
【问题描述】:
我想达到什么目的??
我想使用 API 显示从下拉框中选择的每个项目的价格。我尝试使用点击事件,但现在对我来说太难了,因为我是初学者。当我们选择一个选项时,有人可以帮我在表单左下角的下拉框中显示所选项目的价格吗?
const apiData =[
{
"id":"1",
"question" :"Love",
"price":"90"
},
{
"id":2,
"question":"Carrer",
"price":"80"
},
{
"id":"3",
"question":"Shadi",
"price":"100"
},
{
"id":"4",
"question":"Future",
"price":"200"
},
{
"id":"5",
"question":"Office",
"price":"300"
},
{
"id":"6",
"question":"After-Life",
"price":"400"
}
]
//Js Code for diplaying items in drop box (working prefectly fine)
async function loadUsers() {
//const response = await fetch("../reference_json/questions_API.js");
//return response.json();
return apiData
}
document.addEventListener("DOMContentLoaded", async () => {
try {
const users = await loadUsers();
const divContainer = document.getElementById('question');
users.forEach(user => {
const paragraphElem = document.createElement('option');
paragraphElem.innerText = `${user.question}`;
divContainer.appendChild(paragraphElem);
});
} catch (e) {
console.log('ERROR');
console.log(e);
}
});
<form class ="choose-form">
<div class="mb-3">
<select class="form-select" id ="question">
<option>Select a category : Love,carrer,more ...</option>
</select>
</div>
<nav class="navbar navbar-expand-lg navbar-light">
<span> ₹99(including GST)</span>
<span>Ideas what to ask</span>
<div class="profile-icon" style="justify-content-end">
<button type="submit" class="btn btn-warning">Ask a Question </button>
</div>
</nav>
</form>
【问题讨论】:
-
您希望在哪里显示每个选定商品的价格?
-
在下拉框下方,即左下角,如果放大第一张图片,您会发现 RS XYZ(99)(包括 GST),其中 XYZ 是价格。代替XYZ
标签: javascript html