2013-09-27 52 views
1

//为国家根据jquery自动填充中选择的国家来管理城市?

var objCountries = []; 
var objSearchCountry = new Object(); 
objSearchCountry.CountryName = $("#txtCountry").val(); 
$.ajax({ 
    type: "POST", 
    url: "db.php?GetCountryList", 
    data: {data:[]}, 
    dataType: "json", 
    async:false, 
    success: function(response) 
    { 
     if(response.IsError) 
      alert(response.ErrorMessage); 
     else 
      objCountries = response; 
    }, 
    error:function(response) 
    { 
     alert("Error: " + response.responseText); 
    } 
}); 

var newObjCountry = []; 
for (var indexCountry in objCountries) 
    newObjCountry.push(objCountries[indexCountry].CountryName); 
$("#txtCountry").autocomplete({ source: newObjCountry }); 
当我选择任何一个国家

,我想它的ID,这样我可以通过这个ID,在城市获得相关的城市。

$("#txtCountry").blur(function() 
{ 
//for city 
var objCities = []; 
var objSearch = new Object(); 
objSearch.city_name = $("#txtCity").val(); 
$.ajax({ 
    type: "POST", 
    url: "db.php?GetCityList", 
    data: {data:[]}, 
    dataType: "json", 
    async:false, 
    success: function(response) 
    { 
     if(response.IsError) 
      alert(response.ErrorMessage); 
     else 
      objCities = response; 
    }, 
    error:function(response) 
    { 
     alert("Error: " + response.responseText); 
    } 
}); 

var newObj = []; 
for (var index in objCities) 
    newObj.push(objCities[index].city_name); 
$("#txtCity").autocomplete({ source: newObj }); 
}); 

感谢

+0

您可以使用$( “#txtCountry”)。VAL() ;获取Country val并将其传递给您的GetCityList方法。 –

+0

已尝试但无法运行.. :( –

+0

您不会在任何地方存储国家ID。创建自动填充时,它只包含国家/地区名称。您需要使用select元素。 – Archer

回答

1

你需要的是对自动完成的选择事件中使用

这里是Working Demo

select : function(e, ui){ 
    alert("selected!" + ui.item.value); 
    //rest of the code after selection goes here 
} 
相关问题