2009-12-31 85 views
0

我使用以下代碼來填充下拉:下拉不灌裝動態

$(document).ready(function(){ 
    $('#inDistrict').sSelect(); 
    $("#inDistrict").change(function(){ 
    $.getJSON("filldistricts.php",{id: $(this).val(), ajax: 'true'}, function(j){ 
     var options = ''; 
     for (var i = 0; i < j.length; i++) { 
     options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'; 
     } 
    }) 
    }) 
}) 

該文件的代碼:filldistricts.php是如下:

<?php 
    require_once("../Lib/dbaccess.php"); 
    $query = "SELECT districtid, districtname FROM districtmaster"; 
    try 
    { 
    $result = dbaccess::GetRows($query); 
    echo json_encode($result); 
    } 
    catch(exception $ex) 
    { 
     echo "<script type='text/javascript'>alert('".$ex."')</script>"; 
    } 
?> 

下拉未完全填滿。哪裏有問題?

被修改:

DBAccess.php [GetRows的功能]僅包含以下代碼:

$resultSet = mysql_query($inQuery); 
return $resultSet; 

連接在上述代碼之前打開。

回答

1

您在更改處理程序中創建了options變量,但實際上並未將最終結果放入下拉列表中!

嘗試:

$.getJSON("filldistricts.php",{id: $(this).val(), ajax: 'true'}, function(j){ 
    var options = []; 
    for (var i = 0; i < j.length; i++) { 
    options.push('<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'); 
    } 
    // change #whatever to the id of the dropdown you 
    // want to update, it's not clear from your question 
    $('#whatever').html(options.join('')); 
}) 
+0

僅供參考,'push'然後'join'而不是字符串concatention應該是相當快一點,這就是爲什麼我改變了它。 – rfunduk 2009-12-31 13:30:51

+0

仍然無法正常工作。 – RKh 2009-12-31 13:44:14

+1

你能更具體嗎?後端的結果是否恢復正確?在for循環之後嘗試'console.log(options.join(''))'並確保它看起來正確。你想修改什麼下拉菜單? – rfunduk 2009-12-31 14:38:38