Jquery 下拉列表不动态填充
我使用以下代码填充下拉列表:Jquery 下拉列表不动态填充,jquery,ajax,json,Jquery,Ajax,Json,我使用以下代码填充下拉列表: $(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 <
$(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>';
}
})
})
})
连接在上述代码之前打开 您正在更改处理程序中创建
选项
变量,但实际上根本没有将最终结果放入下拉列表中
尝试:
$.getJSON(“filldistricts.php”,{id:$(this.val(),ajax:'true'},函数(j){
var期权=[];
对于(变量i=0;i
FYI,push
然后join
而不是string concatention应该快一点,这就是我更改它的原因。你能更具体一点吗?后端返回的结果是否正确?在for循环之后尝试console.log(options.join(“”))
,确保它看起来正确。您试图修改的下拉列表是什么?
<?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>";
}
?>
$resultSet = mysql_query($inQuery);
return $resultSet;
$.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('') );
})