閱讀376 返回首頁    go 阿裏雲 go 技術社區[雲棲]


jQuery 省市區多級(三級/四級/五級。。。)聯動 BY 凨來了

/**
 * 省市縣區三級多級聯動
 * author:凨來了
 */
jQuery(function($) {
    var city=[],cityName=[];
    $.fn.city = function (opt) {
        var $id = $(this),
            options = $.extend({
                url: 'data.php?parent_id=',
                /*當前ID,設置選中狀態*/
                selected: null,
                /*上級欄目ID*/
                parent_id: null,
                /*主鍵ID名稱*/
                valueName: "id",
                /*名稱*/
                textName: "cn_name",
                /*默認名稱*/
                defaultName: "-----",
                /*下級對象ID*/
                nextID: null}, opt),selected,_tmp;
        if(options.parent_id==null){
            _tmp=$id.data('parent_id');
            if(_tmp!==undefined){
                options.parent_id=_tmp;
            }
        }
        //初始化層
        this.init = function () {
            if($.inArray($id.attr('id'),cityName)==-1){
                cityName.push($id.attr('id'));
            }
            if(!options.selected){
                options.selected=$id.data('selected');
            }
            $id.append(format(get(options.parent_id)));
        };
        function get(id) {
            if (id !== null && !city[id]) {
                getData(id);
                return city[id];
            }else if (id !== null && city[id]) {
                return city[id];
            }
            return [];
        }

        function getData(id) {
            $.ajax({
                url: options.url+ id,
                type: 'GET',
                async: false,
                success: function (d) {
                    if (d.status == 'y') {
                        city[id] = d.data;
                    }
                }
            });
        }

        function format(d) {
            var _arr = [], r, selected = '';
            if (options.defaultName !== null) _arr.push('<option value="">' + options.defaultName + '</option>');
            if ($.isArray(d)) for (var v in d) {
                r = null;
                r = d[v];
                selected = '';
                if (options.selected && options.selected == (r[options.valueName])) {
                    selected = 'selected';
                }
                _arr.push('<option value="' + r[options.valueName] + '" ' + selected + '>' + r[options.textName] + '</option>');
            }
            return _arr.join('');
        }

        this.each(function () {
            options.nextID && $id.on('change', function () {
                var $this = $('#' + options.nextID),id=$(this).attr('id'),i=$.inArray(id,cityName);
                $this.html(format(get($(this).val())));
                if ($.isArray(cityName)) for (var v in cityName) {
                    if(v>(i+1)){
                        $('#'+cityName[v]).html(format());
                    }
                }
            });
        });
        this.init();
    }
});

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>jQuery 省市區多級(三級/四級/五級。。。)聯動 BY 凨來了</title>
</head>
<body><dl>
    <dt>所在地區:</dt>
    <dd>
        <select  name="province"  data-selected="10" data-parent_></select>
        <select  name="city"  data-selected="112" data-parent_></select>
        <select  name="county"  data-selected="1387" data-parent_></select>

    </dd>
</dl>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="city.js"></script>
<script type="text/javascript">
    $(function(){
        $('#province').city({nextID:'city'});
        $('#city').city({nextID:'county'});
        $('#county').city();
    });
</script>
</body>
</html>
demo下載

最後更新:2017-04-03 07:57:05

  上一篇:go Hadoop簡介
  下一篇:go 利用objc的runtime來定位次線程中unrecognized selector sent to instance的問題