【问题标题】:Chained Drop Down values not changing after first drop down value has been changed更改第一个下拉值后,链式下拉值未更改
【发布时间】:2014-03-29 10:17:22
【问题描述】:

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

我的 Js 文件:

$(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);

    }


    });

}

javascript中有没有像asp.net这样的Autopost back属性??

【问题讨论】:

  • 控制台有错误吗?是打法吗?
  • 一切似乎都很好,我得到了正确的数据...正如我在问题中提到的,它第一次运行良好。但是在第二次更改国家/地区名称后,相应的更改并未反映在剩余的 2 个下拉菜单中

标签: jquery


【解决方案1】:

这可能会发生,因为我在您所在国家/地区的部分添加了 return 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-01-02
    • 2012-08-17
    • 1970-01-01
    • 2021-11-09
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多