• 省市区三级联动插件:app-jquery-cityselect.js


    (function ($)
    {
        $.fn.cityselect = function (options)
        {
            var settings = $.extend ({}, options);
            
            this.empty ();
            
            var provinceId, provinceName, cityId, cityName;
            
            if (settings.loadDefault)
            {
                var defaultData = settings.loadDefault ();
                
                provinceId = defaultData.provinceId;
                provinceName = defaultData.provinceName;
                cityId = defaultData.cityId;
                cityName = defaultData.cityName;
            }
            
            var provinceInput = $ ('<select class="form-control" style="150px"></select>');
            var cityInput = $ ('<select class="citySel form-control" style=" 150px;margin-left: 10px"></select>');
            
            this.addClass ("input-append");
            this.append (provinceInput);
            this.append (cityInput);
            
            if (settings.loadProvince)
            {
                
                var provinceList = settings.loadProvince ();
                $.each (provinceList, function (i, p)
                {
                    if (i == 0)
                    {
                        if (settings.areaInput)
                        {
                            settings.areaInput.val (p.id);
                        }
                        if (settings.onAreaIdChanged)
                        {
                            settings.onAreaIdChanged (p.id);
                        }
                        if (settings.onProvinceChanged)
                        {
                            settings.onProvinceChanged (p.id, p.name);
                        }
                    }
                    var node = $ ('<option value="{0}">{1}</option>'.format (p.id, p.name));
                    if (provinceId && p.id == provinceId)
                    {
                        node.attr ("selected", "selected");
                        setProvinceInput (p.id);
                    }
                    provinceInput.append (node);
                });
            }
            else
            {
                throw "必须设置 loadProvince 回调函数";
            }
            
            provinceInput.on ("change", function (e, d)
            {
                var selected = $ (this).find ("option:selected");
                
                if (settings.areaInput)
                {
                    settings.areaInput.val (selected.val ());
                }
                if (settings.onAreaIdChanged)
                {
                    settings.onAreaIdChanged (selected.val ());
                }
                if (settings.onProvinceChanged)
                {
                    settings.onProvinceChanged (selected.val (), selected.text ());
                }
                
                setProvinceInput (selected.val ());
            });
            
            function setProvinceInput (provinceId)
            {
                if (settings.loadCity)
                {
                    
                    var cityList = settings.loadCity (provinceId);
                    
                    cityInput.empty ();
                    $.each (cityList, function (i, p)
                    {
                        if (i == 0)
                        {
                            if (settings.areaInput)
                            {
                                settings.areaInput.val (p.id);
                            }
                            if (settings.onAreaIdChanged)
                            {
                                settings.onAreaIdChanged (p.id);
                            }
                            if (settings.onCityChanged)
                            {
                                settings.onCityChanged (p.id, p.name);
                            }
                        }
                        var node = $ ('<option value="{0}">{1}</option>'.format (p.id, p.name));
                        if (cityId && p.id == cityId)
                        {
                            node.attr ("selected", "selected");
                        }
                        cityInput.append (node);
                        
                    });
                    
                }
                else
                {
                    throw "必须设置 loadCity 回调函数";
                }
                
            }
            
            cityInput.on ("change", function (e, d)
            {
                var selected = $ (this).find ("option:selected");
                
                if (settings.areaInput)
                {
                    settings.areaInput.val (selected.val ());
                }
                if (settings.onAreaIdChanged)
                {
                    settings.onAreaIdChanged (selected.val ());
                }
                if (settings.onCityChanged)
                {
                    settings.onCityChanged (selected.val (), selected.text ());
                }
            });
            
            return this;
            
        };
        
    }) (jQuery);
  • 相关阅读:
    Atcoder D
    51nod 1201 整数划分(dp)
    Atcoder D
    Atcoder C
    codeforces 812 E. Sagheer and Apple Tree(树+尼姆博弈)
    codeforces 811 D. Vladik and Favorite Game(bfs水题)
    codeforces 811 E. Vladik and Entertaining Flags(线段树+并查集)
    codeforces 811 C. Vladik and Memorable Trip(dp)
    1449 砝码称重(思维)
    SQL大量数据查询的优化 及 非用like不可时的处理方案
  • 原文地址:https://www.cnblogs.com/cryst/p/6029159.html
Copyright © 2020-2023  润新知