selectCity.js 6.36 KB
/**
 * 选择城市插件
 * 使用:
 * 1. 创建一个DIV做为容器, 然后设置变量
 *   var config = {
            box: "#cityBox"
            , names: {
                province : "obj.enterprise.province"
                , city: "obj.enterprise.city"
                , county: "obj.enterprise.county"
                , text: "obj.address"
            }
            ,vals: [
                151, 3001, 1510103
            ]
        }

 var sCity = $.selectCity(config);
 */
(function ($) {

    var config = {
        //存放列表的容器
        box: ""
        //各个下拉的名字
        , names: {
            //省
            province : "province"
            //市
            , city: "city"
            //区
            , county: "county"
            //文本
            , text: ""
        }
        //默认值
        ,vals:[]
        //请求地址
        , url: location.protocol + "//" + location.hostname + "/regedit/cityInformation?parentId="
    }


    var html = '<select class="citySelect u-sel-addr" id="address_province" name="$province" sublevel="address_city"></select>'
                + '<select class="citySelect u-sel-addr" id="address_city" name="$city" sublevel="address_county"></select>'
                + '<select class="citySelect u-sel-addr" id="address_county" name="$county"></select>'
                + '<input type="hidden" name="$text" id="address_text" value=""/>';


    var sCity = {
        /**
         * 初始化列表
         * 根据父级ID号获取列表
         * 同时根据列表设置默认值
         */
        fillList: function (list, id, vals) {
            var self = this;
            var tempUrl = config.url;
            if (id) {
                tempUrl += id;
            }
            var def = "";
            if(vals instanceof Array && vals && vals.length > 0){
                def = vals.shift();
            }

            $.ajax({
                url: tempUrl, dataType: "jsonp", success: function (data) {
                    self.cleanAddress(list);
                    for (var i = 0; i < data.length; i++) {
                        var d = data[i];
                        var selected = ""
                        if(def == d.regionId){
                            selected = "selected"
                        }
                        var option = $("<option value='" + d.regionId + "' "+selected+">" + d.regionName + "</option>");
                        list.append(option);
                    }

                    self.fillNext(list, def, vals);
                    self.refreshText();
                }, error: function (e) {
                }
            });
        }
        /**
         * 填充下一级
         * @param obj
         * @param val
         * @param def
         */
        ,fillNext: function(obj, val, def){
            var self = this;
            var sub = self.fetchSub(obj);
            if (sub) {
                if (val == null || val == "") {
                    self.cleanAddress(sub);
                    return;
                }
                self.fillList(sub, val, def);
            }
        }
        /**
         * 清除下拉
         * @param list
         */, cleanAddress: function (list) {
            var self = this;
            list.html("");
            
            if(list.attr("id") == "address_province"){
            	list.append("<option value=''>请选择省</option>");
            }else if(list.attr("id") == "address_city"){
            	list.append("<option value=''>请选择市</option>");
            }else{
            	list.append("<option value=''>请选择区/县</option>");
            }
            
            var sub = self.fetchSub(list);
            if (sub) {
                self.cleanAddress(sub);
            }
        }

        /**
         * 清除所有
         */, cleanAllAddress: function () {
            var self = this;
            self.fillList($("#address_province"));
        }

        /**
         * 下级下拉列表
         * @param obj
         * @returns {*|jQuery|HTMLElement}
         */, fetchSub: function (obj) {
            var subLevel = $(obj).attr("sublevel");
            if (subLevel) {
                var sub = $("#" + subLevel);
                return sub;
            }
        }
        /**
         * 插入HTML
         */
        , insertHtml: function () {
            var box = $(config.box);
            var self = this;
            var tag = html;
            tag = tag.replace("$province", config.names.province);
            tag = tag.replace("$city", config.names.city);
            tag = tag.replace("$county", config.names.county);
            tag = tag.replace("$text", config.names.text);
            box.append(tag);

            self.obj = {}
            self.obj.province = $("#address_province");
            self.obj.city = $("#address_city");
            self.obj.county = $("#address_county");
            self.obj.text = $("#address_text");
        }
        /**
         * 绑定事件
         */, bind: function () {
            var self = this;
            $(".citySelect").each(function () {
                $(this).change(function () {
                    var val = $(this).val();
                    var obj = $(this);
                    self.fillNext(obj, val);
                    self.refreshText();
                });
            });
        }
        /**
         * 刷新文本
         */
        , refreshText: function(){
            var self = this;
                var po = self.obj.province;
                var co = self.obj.city;
                var yo = self.obj.county;
            var txt = self.obj.text;
                var address = "";

            if(po.val() && po.val != ""){
                address += po.find("option:selected").text();
            }
            if(co.val() && co.val() != ""){
                address += co.find("option:selected").text();
            }
            if(yo.val() && yo.val != ""){
                address += yo.find("option:selected").text();
            }
            txt.val(address);
        }
        /**
         * 初始化
         */
        , init: function () {
            this.insertHtml();
            this.bind();
            var dv = config.vals;
            this.fillList($("#address_province"), "", dv);
        }
    };


    $.selectCity = function (cfg) {
        config = $.extend(config, cfg);
        var t = sCity;
        t.init();
        return t;
    };
})(jQuery);