JS 省市联动 ajax

省市联动

//省市联动

    $('.locationCode').change(function () {

        var val = $(this).val();

        if (!val) return false;

        var url = '/talentproject/regions?provinceCode=' + val;

        $.ajax({

            url: url,

            success: function (data) {

                if (!data) {

                    alert('获取失败,请重新获取!');

                }

                var html = '<option value="">请选择市</option>';

                for (var i in data) {

                    html += '<option value=' + data[i].code + '>' + data[i].name + '</option>';

                }

                $('.cityCode').empty();

                $('.cityCode').append(html).trigger("click");

                //获取后台给的城市代码

                var cityCode = $("#thcityCode").text();

                //循环判断项目所属城市

                $('.cityCode option').each(function () {

                    if ($(this).val() == cityCode) {

                        $(this).attr('selected', true);

                    }

                });

            }

        });

    });

    if($('.locationCode').val()){

        $('.locationCode').trigger('change');

    }

    
 

 

文章来源:刘俊涛的博客

欢迎关注,有问题一起学习欢迎留言、评论

上一篇:练习: 省市联动(Ajax)


下一篇:The Engine Document of JustWeEngine