根据在jquery自动完成中选择的国家/地区管理城市?

时间:2013-09-27 07:22:16

标签: jquery

// for country

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 });
});

由于

1 个答案:

答案 0 :(得分:1)

您需要的是使用自动完成的选择事件

这是Working Demo

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