第一个下拉值更改后,链式下拉值不会更改

时间:2014-03-29 10:17:23

标签: jquery

我使用json数据将国家/地区列表,状态列表城市列表绑定到下拉列表。我的要求是基于国家的状态应该改变。根据各州,城市价值观应该改变。假设如果我第一次选择印度各自的州,但是在我更改国家/地区的国家/地区后,新的值不会在状态下拉列表中更新,我会在firebug中获得正确的状态数据。

我的Jsfile:

$(document).ready(function () {
  bindData();
  BindCountry();
  var DropDown1 = $("#ddlCountry");
  DropDown1.change(function (e) {
    var CountryCode = DropDown1.val();
    if (CountryCode >= 1) {

        GetStates(CountryCode);

    }

  });

 });


 function BindCountry() {

var Dropdown1 = $("#ddlCountry");
$.ajax({
    type: "POST",
    url: location.pathname + "/GetCountry",
    data: "{}",
    contentType: "application/json;charset=utf-8",
    datatype: "json",
    success: function (response) {
        var country = response.d;
        $.each(country, function (index, country) {
            Dropdown1.append('<option value="' + country.CountryCode + '">' + country.Country + '</option>');

        });
    },
    failure: function (msg) {
        alert(msg);

       }

   });

  }


function GetStates(Coun_code) {
    var DdlState = $("#ddlState");
    $.ajax({
    type: "POST",
    url: location.pathname + "/GetStates",
    data: "{'CountryCode':'" + Coun_code + "'}",
    contentType: "application/json;charset=utf-8",
    datatype: "json",
    success: function (response) {
    var state = response.d;
    $.each(state, function (index, state) {
    DdlState.append('<option value="' + state.StateCode + '">' + state.StateName + '</option>');

       });

      },
  failure: function (msg) {

    alert(msg);

    }


    });

}

是否有像asp.net这样的Autopost返回属性在javascript中?

1 个答案:

答案 0 :(得分:-1)

这可能会发生,因为我在您所在国家/地区的部分添加了返回false,所以请删除此。我没有尝试过这个

        $(document).ready(function () {
          bindData();
          BindCountry();
          var DropDown1 = $("#ddlCountry");
          DropDown1.change(function (e) {
            var CountryCode = DropDown1.val();
            if (CountryCode >= 1) {

                GetStates(CountryCode);

            }

          });

         });


         function BindCountry() {

        var Dropdown1 = $("#ddlCountry");
        $.ajax({
            type: "POST",
            url: location.pathname + "/GetCountry",
            data: "{}",
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function (response) {
                var country = response.d;
                $.each(country, function (index, country) {
                    Dropdown1.append('<option value="' + country.CountryCode + '">' + country.Country + '</option>');

                });
            },
            failure: function (msg) {
                alert(msg);

               }

           });
       t
          }


        function GetStates(Coun_code) {
            var DdlState = $("#ddlState");
            $.ajax({
            type: "POST",
            url: location.pathname + "/GetStates",
            data: "{'CountryCode':'" + Coun_code + "'}",
            contentType: "application/json;charset=utf-8",
            datatype: "json",
            success: function (response) {
            var state = response.d;
            $.each(state, function (index, state) {
            DdlState.append('<option value="' + state.StateCode + '">' + state.StateName + '</option>');

               });

              },
          failure: function (msg) {

            alert(msg);

            }


            });
        return false;
        }

    Hope this will work.