简体   繁体   English

如何在javascript中只显示HTML select框中当前选中的数据?

[英]How to display in javascript only the current selected data from HTML select box?

The following code displays results data from the table according to the choice from HTML select box, but it appends the new results to the previous one.以下代码根据来自 HTML select 框的选择显示表中的结果数据,但它将新结果附加到前一个结果。 How to display only the current selected data?如何只显示当前选中的数据?

 <script> function GetSelectedText() { const trips = [ ["AAA", "London"], ["AAA", "Manchester"], ["AAA", "Oxford"], ["BBB", "Paris"], ["BBB", "Lyon"], ["BBB", "Marsylia"], ["CCC", "Berlin"], ["CCC", "Hamburg"], ["CCC", "Bonn"] ] var oSel = document.getElementById("country"); var wybrano = oSel.options[oSel.selectedIndex].text; var parent = document.getElementById("wybrano"); for (var i = 0; i < 9; i++) { if (wybrano == trips[i][0]) { divy = document.createElement("div"); divy.innerHTML = '<p>' + i + ' ' + trips[i][1] + '</p>'; document.body.appendChild(divy); } } } </script> <form id="Forma"> <select id="country" onChange="GetSelectedText()"> <option value="0"> country </option> <option value="1"> AAA </option> <option value="2"> BBB </option> <option value="3"> CCC </option> </select> </form> <div id="content"></div>

In your code, you didn't append the results to content div, but you append it to body instead.在您的代码中,您没有 append 结果到内容div,而是 append 到正文。 It's better to wrap all the result in a div.最好将所有结果包装在一个 div 中。 On every OnChange event, simply reset the html/clear the div.在每个OnChange事件中,只需重置 html/清除 div。

 <script> function GetSelectedText() { const trips = [ ["AAA","London"],["AAA","Manchester"],["AAA","Oxford"], ["BBB","Paris"],["BBB","Lyon"],["BBB","Marsylia"], ["CCC","Berlin"],["CCC","Hamburg"],["CCC","Bonn"] ] var oSel = document.getElementById("country"); var wybrano = oSel.options[oSel.selectedIndex].text; var content = document.getElementById('content'); //Remove Previous Content //const myNode = content; //while (myNode.firstChild) { // myNode.removeChild( myNode.lastChild ); //} //while (myNode.lastElementChild) { // myNode.removeChild(myNode.lastElementChild); //} //Or Simply content.innerHTML = ""; for (var i=0; i < 9; i++) { if ( wybrano == trips[i][0]) { divy = document.createElement("div"); divy.innerHTML = '<p>'+i+' '+trips[i][1]+'</p>'; content.appendChild( divy ); } } } </script> <form id="Forma"> <select id="country" onChange="GetSelectedText()"> <option value="0"> country </option> <option value="1"> AAA </option> <option value="2"> BBB </option> <option value="3"> CCC </option> </select> </form> <div id="content"> </div>

Here is another slightly different approach.这是另一种略有不同的方法。 I pre-process the trips array first into an object that will then return bits of HTML to be joined and put into the content.innerHTML :我首先将 trips 数组预处理为 object,然后返回 HTML 的位以加入并放入content.innerHTML中:

 const trips = [ ["AAA","London"],["AAA","Manchester"],["AAA","Oxford"], ["BBB","Paris"],["BBB","Lyon"],["BBB","Marsylia"], ["CCC","Berlin"],["CCC","Hamburg"],["CCC","Bonn"] ].reduce((a,[k,t],i)=>((a[k]=a[k]||[]).push("<p>"+i+" "+t+"</p>"),a),{}), oSel = document.getElementById("country"), content=document.getElementById("content"); function GetSelectedText() { content.innerHTML = (trips[oSel.options[oSel.selectedIndex].text]?? ["no selection made"]).join(""); }
 <form id="Forma"> <select id="country" onChange="GetSelectedText()"> <option value="0"> country </option> <option value="1"> AAA </option> <option value="2"> BBB </option> <option value="3"> CCC </option> </select> </form> <div id="content"> </div>

I. tried to clear the results of the previous selections by inneHTML and/or removing the child element with the folowing code before the for loop ie. I. 尝试通过 inneHTML 清除先前选择的结果和/或在 for 循环之前使用以下代码删除子元素,即。 for (var i=0; i < 9; i++) responsible for the display (but not succeded yet): for (var i=0; i < 9; i++) 负责显示(但尚未成功):

  document.getElementById("content").innerHTML = ""; 
  const myNode = document.getElementById("content");
  while (myNode.firstChild) {
         myNode.removeChild( myNode.lastChild );
  } 
  while (myNode.lastElementChild) {
         myNode.removeChild(myNode.lastElementChild);
  }

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

相关问题 使用jQuery从选择框中删除选择的值,但需要在选择框中显示当前选择的值吗? - remove selected value from select box using jquery but It's need to display current selected value in select box? 如何显示从Javascript到Select Statement HTML的数据列表? - How to display a list of data from Javascript into a Select Statement HTML? 使用php从mysql中选择数据,显示在html下拉列表中,并通过javascript插入所选值作为URL参数 - Select data from mysql with php, display in html dropdown list and insert selected value as URL parameter via javascript 如何使用Javascript Prototype从多个选择框中删除所选选项? - How to remove selected options from multiple select box with Javascript Prototype? 从选择框中获取选定的HTML文件 - Fetching the selected options html from select box 通过 HTML 从 select 框中打印所选信息 - print selected information from select box by HTML 从javascript中的选择框中选择值 - selected value from select box in javascript 如何将选定的数据从列表添加到多个选择框? - How to add selected data from list to multiple select box? 如何将警报框的输出转换为在页面上显示-javascript / html - how to convert output from alert box to display on page - javascript/html 如何使用HTML和JavaScript从表单框中显示URL? - How to display a URL from a form box using HTML and a javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM