zoukankan      html  css  js  c++  java
  • 省市区三级联动插件: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);
  • 相关阅读:
    hdu 5056 Boring count (窗体滑动)
    【JEECG技术博文】JEECG表单配置-树形表单
    EA类图与代码同步
    简单算法汇总
    jQuery.outerWidth() 函数具体解释
    省市县三级联动的SQL
    从LayoutInflater分析XML布局解析成View的树形结构的过程
    holodesk表存储-列重复值太多,导致存储占用空间翻很多倍
    星环TDH大数据平台,数据误删除之后,恢复办法
    IDEA类和方法注释模板设置(非常详细)
  • 原文地址:https://www.cnblogs.com/cryst/p/6029159.html
Copyright © 2011-2022 走看看