【问题标题】:Display drop box item selected price from API显示从 API 中选择的下拉框项目价格
【发布时间】: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


【解决方案1】:

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 oSel = document.querySelector('select[name="question"]');
    
    users.forEach(user => {
      let option=new Option( user.question, user.id );
          option.dataset.price=user.price;
      oSel.appendChild( option );
    });
    
    oSel.addEventListener('change',function(e){
      let selection=this.options[this.options.selectedIndex];
      document.querySelector('nav.navbar > span:nth-of-type(2)').innerText=[
        selection.dataset.price,
        selection.text
      ].join(' ');
    });
    
  } catch (e) {
    console.log(e);
  }
});
<form class="choose-form">
        
        
  <div class="mb-3">
    <select  class="form-select" id="question" name='question'>
      <option>Select a category : Love, carrer, more ...
    </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>

【讨论】:

  • 谢谢阿布罗修斯教授!!!!!!这正是我想要的
【解决方案2】:

由于您从 ajax 调用数据,因此您必须在从服务器加载数据时保存所有需要的值,然后在 select 元素上绑定 onchange 事件,然后将所选选项中的值打印到您的 span

下面是更正后的代码

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}`;
    // edited line saving matching data to option
    paragraphElem.data = user
    divContainer.appendChild(paragraphElem);
  });
} catch (e) {
  console.log('ERROR');
  console.log(e);
}
});

// edited line
var select = document.querySelector("#question"),
  priceEl = document.querySelector(".priceShow")
select.onchange = function(){
    var selected = select[select.selectedIndex],
        value = typeof selected.data != "undefined" ? selected.data.price:null
    if(value){priceEl.innerText = `₹${value}(including GST)`}
}
<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">   
    <!-- edited line -->
      <span class="priceShow"> ₹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>

【讨论】:

  • @Natasha Anderson 是的,实际上我在发布答案后看到了,所以我只是在编辑它,现在检查一下
  • 谢谢阿米尔!!!!
  • @Natasha Anderson 祝你好运
猜你喜欢
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多