2014-02-28 58 views
0

下面的代碼嘗試通過在服務器上連接到PHP的Ajax調用並檢索數據來更改Select選項標記。但是,如果我要將響應文本設置爲span標記,但它不適用於Select選項標記,則下面的代碼工作正常嗎?選擇選項標記不會使用ajax/php調用更改?

<?php 

$q = intval($_GET['q']); 

$con=mysqli_connect("localhost","rot","ro","abc","3306"); 

/

if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 


$result = mysqli_query($con,"SELECT DISTINCT(UniqueBusId) FROM cfv_busstopnames WHERE busnumber = ".$q." "); 


if (!$result) { 
    printf("Error: %s\n", mysqli_error($con)); 
    exit(); 
} 


while($row = mysqli_fetch_array($result)) 
    { 
    echo "<option>" . $row['UniqueBusId'] . "</option>" ; 

    } 

mysqli_close($con); 


?> 

。取而代之的

xmlhttp.onreadystatechange=function(){ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     document.getElementById("select-choice-direction").innerHTML=xmlhttp.responseText; 
    } 
} 

<script> 
    function showUser(str) 
    { 

    if (str=="") 
    { 
    document.getElementById("txtHint").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("select-choice-direction").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","getdirection.php?q="+str.value,true); 
    xmlhttp.send(); 
    } 


</script> 
<Body> 
    <label for="select-choice-direction">Direction</label> 
    <select name="select-choice-direction" id="select-choice-direction" data-native-menu="false" "> 
    <option> Direction heading towards?</option> 
    <option value="X">X</option> 
    <option value="Y">Y</option> 
    <!-- etc. --> 
    </select> 
</Body> 
+0

你爲什麼要給jQuery加標籤?你如何觸發你的功能? – putvande

回答

1

嘗試

xmlhttp.onreadystatechange=function(){ 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
     $('#select-choice-direction').html(xmlhttp.responseText).selectmenu("refresh"); 
    } 
} 

這告訴jQuery Mobile的刷新selectmenu部件(http://api.jquerymobile.com/selectmenu/#method-refresh

DEMO

+0

感謝百萬的解釋 –

+0

不客氣! – ezanker