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