2014-06-09 47 views
0

我只需要一個事件或我的代碼的動作,當值爲「0」時不顯示信息。通常當值大於1時,它會顯示XML信息,即使選擇了X,它也會顯示以前的XML數據,但我希望它是空的。不知道如何隱藏一個0值的信息

<!DOCTYPE html> 
<html> 
<head> 
<link href="style.css" rel="stylesheet" /></head> 
<body> 

<form> 
<select id="pullDownList" onchange="myFunction();"> 
<option value="0">Please Choose a Country</option> 

<script> 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","./cords_data.xml",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 


var xmlDocument=xmlDoc.getElementsByTagName("Row"); 
for (i=0;i<xmlDocument.length;i++) 
{ 
document.write("<option value='"); 
document.write(i+1); 
document.write("'>"); 
document.write(xmlDocument[i].getElementsByTagName("Country")[0].childNodes[0].nodeValue); 
} 
</script></select> 
<p /> 

<div id=fieldInfo1></div> 
<div id=fieldInfo2></div> 
<div id=fieldInfo3></div> 
<div id=fieldInfo4></div> 
</p> 

<script> 

function myFunction() 


var z = document.getElementById("pullDownList").selectedIndex-1; 
if (pullDownList.value == "0"){ 



}else { 

document.getElementById("fieldInfo1").innerHTML = xmlDocument[z].getElementsByTagName("Country")[0].childNodes[0].nodeValue; 
document.getElementById("fieldInfo2").innerHTML = xmlDocument[z].getElementsByTagName("Voltage")[0].childNodes[0].nodeValue; 
document.getElementById("fieldInfo3").innerHTML = xmlDocument[z].getElementsByTagName("Freq__Hz")[0].childNodes[0].nodeValue; 
document.getElementById("fieldInfo4").innerHTML = xmlDocument[z].getElementsByTagName("Cord_Designator")[0].childNodes[0].nodeValue; 






} 
</script> 

回答

0

嘗試這樣做,而不是:

if (document.getElementById("tagThatHoldsValueOfZero").innerHTML == "0") { 

document.getElementById("tagThatHoldsValueOfZero).style.display = "none"; 
} 

使用.innerHTML將獲得該元素的含量/文本。

+0

然後XML的東西應該只是在其他下的呢? – shaysilv

+0

正確。我不確定你什麼時候想要這個事件觸發(例如:在頁面加載,按鈕點擊,字段改變等),但在我的眼中,在頁面加載時,你會想檢查零值,然後執行你想爲這些領域發生什麼。所以,'else'聲明應該涵蓋它。 – mwilson

+0

非常感謝!它的工作原理,我很感激 – shaysilv