简体   繁体   English

有没有办法使用选定的选项更改HTML显示,从下拉菜单中不刷新?

[英]Is there a way to change HTML display with a selected option, from drop down menu without refreshing?

I have a table that gets data from xml files. 我有一个从xml文件中获取数据的表。 Three different options (files) that can be chosen from a drop down menu. 可从下拉菜单中选择三种不同的选项(文件)。 When one option is chosen, the table gets the data, but when another option is chosen, the table displays both files data unless i refresh the page. 选择一个选项时,表将获取数据,但是当选择另一个选项时,除非我刷新页面,否则表格将显示两个文件数据。 right now it works if I refresh after choosing a new option. 现在,如果我在选择新选项后刷新它是有效的。

I've tried different if and else statements to load the files when an option is chosen. 当选择一个选项时,我尝试了不同的if和else语句来加载文件。 And I have tried to have a default blank option chosen but it doesn't do much. 我试图选择一个默认的空白选项,但它没有做太多。

HTML HTML

  <div id="easy">Easy
    <select name="easy" id="easyS" onclick="getNumbers()">
      <option value="easy1">puzzle 1</option>
      <option value="easy2">puzzle 2</option>
      <option value="easy3">puzzle 3</option>
    </select>
  </div><br>

 <table class="grid">
 <tr id="1">
    <td class="numer1"></td>
    <td class="number"></td>
    <td class="numer2"></td>
    <td class="numer1"></td>
    <td class="number"></td>
    <td class="numer2"></td>
    <td class="numer1"></td>
    <td class="number"></td>
    <td class="numer2"></td>
 </tr>

JS JS

 function getNumbers() {
//this sends a request for the local XML file.
 var request = new XMLHttpRequest();
//if statement to get first puzzle mxl document.
if (document.getElementById("easyS").value == "easy1" || 
    document.getElementById("hardS").value == "hard1"){
      request.open("GET", "numbers.xml", false);
      request.send(null);

//this gets information from the XML file using the tags.
var xmldoc = request.responseXML;

var numbers = xmldoc.getElementsByTagName("number");
//this will take the information in the tags and store it in an array
var numArray = [];
for(var i = 0; i < numbers.length; i++){
    numArray.push(numbers[i].firstChild.data)
}
//here the class attribute will be used to display the information
//into the grid.
var printNum = document.getElementsByClassName("number");
x = printNum.length;
if (document.getElementById("easyS").value == "easy1"){
    while(x--){
    printNum[x].innerHTML = numArray[x];
    var cells = document.getElementsByTagName('td');
for(var prop in cells){
    if(cells[prop].innerHTML === ''){//this will only allow numbers in the text field, and a limit of one number at a time.
        cells[prop].innerHTML += '<input type="text"  onkeypress="return event.charCode >= 48 && event.charCode <= 57" maxlength="1"/>'
    }
 }
}   

I want to be able to choose one option and display the numbers in my table, then chose another option and have the information in the table gone and display the new information without having to refresh. 我希望能够选择一个选项并在我的表中显示数字,然后选择另一个选项并使表中的信息消失并显示新信息而无需刷新。

你可以使用更新面板,它将帮助你不再重新加载页面,你必须在控制器之前放置updatepanel(例如:gridview,html table,dropdownlist..etc)我认为它会对你有所帮助。

when you append your numb array in Js, make sure you make it empty on every change of element. 当您在Js中追加您的numb数组时,请确保在每次更改元素时将其设置为空。 so that you have new records every time you change your dropdown value 这样每次更改下拉值时都会有新记录

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

相关问题 如何基于从下拉菜单中选择的选项以HTML显示文本? - How do I display Text in HTML based on an option selected from a Drop-Down Menu? 在下拉菜单中未选择任何选项 - No option selected in drop down menu 使用JQuery更改下拉菜单的选定选项 - Using JQuery to change a drop-down menu's selected option 选择下拉菜单中的选项时无法获取图像以显示图像? - Can't get image to display images when option from drop down menu is selected? HTML。 隐藏/显示下拉菜单,具体取决于是否在另一个下拉菜单中选择了一个选项 - HTML. Hide/Show a drop down menu depending on if an option is selected on another drop down menu 如何从html下拉列表菜单中仅打印一个选项的选定值 - How to print the selected value of only one option from html drop down list menu HTML-如何显示和将选定的值从下拉菜单定向到新的HTML页面 - HTML - How To Display & Direct Selected Values From Drop Down Menu To A New HTML Page 根据从其他组合框中选择的选项更改下拉选项 - Change the drop down option based on the selected option from other combobox 从下拉菜单中选择的选项启用/禁用文本框 - Enable/disable of textbox on option selected from drop down menu 根据从下拉菜单中选择的选项创建查询字符串 - Creating a querystring based on what option is selected from a drop down menu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM