2011-07-12 28 views
0

我正在使用JavaScript onChange,MySQL和PHP來顯示從縣列表填充的城鎮列表中的分支地址。IE瀏覽器不使用onChange呈現選擇選項使用JavaScript,PHP

它可以在除IE(任何版本)以外的所有瀏覽器中正常工作。在IE中我所得到的是一個空白的選擇選項。

JavaScript的

function getCounty(strURL) { 
    if (strURL==="") { 
     document.getElementById("branch").innerHTML=""; 
     return; 
    } 
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState===4 && xmlhttp.status===200) { 
      document.getElementById("branch").innerHTML=xmlhttp.responseText; 
      //alert(document.getElementById("branch").innerHTML=xmlhttp.responseText); 
     } 
    }; 
    xmlhttp.open("GET",strURL,true); 
    xmlhttp.send(null); 
} 

function getBrach(strURL) { 
    if (strURL==="") { 
     document.getElementById("branch-addr").innerHTML=""; 
     return; 
    } 
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { // code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState===4 && xmlhttp.status===200) { 
      document.getElementById("branch-addr").innerHTML=xmlhttp.responseText; 
     } 
    }; 
    xmlhttp.open("GET",strURL,true); 
    xmlhttp.send(null); 
} 

的形式

<form method="post" id="addr-finder" action=""> 
<div class="county"> 
    <label>Choose County</label> 
    <select name="county" id="county" onChange="getCounty('inc/findcity.php?c='+this.value)"> 
     <option value="Select County">Select County</option> 
<?php 
while($row = $result->fetch_object()) { 
    echo '<option value="'.$row->address_5.'">'.$row->address_5.'</option>'; 
} 
?> 
    </select> 
</div> 
<div class="branch"> 
    <label>Choose Branch</label> 
    <select name="branch" id="branch" onChange="getBrach('inc/findbranch.php?t='+this.value)"> 
     <option>--</option> 
    </select> 
</div> 
</form> 

的PHP

<?php 
include("cfg.php"); 
include("functions.php"); 

$county = $_REQUEST['c']; 
$result2 = get_town($county); 
?> 
    <option>Select Town</option> 
<?php 
while($row2 = $result2->fetch_object()) { 
?> 
    <option value="<?php echo $row2->address_4 ; ?>"><?php echo $row2->address_4; ?></option> 
<?php 
} 
?> 
+2

雖然這並不直接回答你的問題,但我建議你開始使用一個隱式處理瀏覽器問題的JavaScript框架。 –

+1

謝謝你@Shamim ....如果我知道這意味着什麼,那真的很有幫助.... –

+1

他的意思是看看一個類似'jQuery'的庫來處理你的事件處理和AJAX。快速閱讀jQuery網站應該解釋這意味着什麼,但基本上它減少了您必須編寫的代碼,併爲您處理大多數瀏覽器問題。 – shanethehat

回答

0

我做到了快速和骯髒的方式。
改爲使用onclick
你甚至可以同時做這兩件事,我這樣做是爲了製作一些移動瀏覽器的一些地方。

相關問題