城市选择(仿jd)

HTML代码:

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>address</title>    <link rel="stylesheet" href="resources/css/address.css"/></head><body><!--传入初始值--><div class="distpicker">    <span class="address-title triangle">选择地址</span>

<div class="address-container" id="address-container" style="display: none"></div></div><br/><br/><br/><br/><!--未初始值--><div class="distpicker">    <span class="address-title triangle">选择地址</span>

<div class="address-container" id="address-container1" style="display: none"></div></div><script type="text/javascript" charset="utf-8" src="resources/js/jquery-1.7.2.min.js"></script><script type="text/javascript" charset="utf-8" src="resources/js/api_areas.js"></script><script type="text/javascript" charset="utf-8" src="resources/js/getCity.js"></script><script>    $(function () {        $(‘.distpicker‘).on({            mouseover: function () {                $(this).addClass(‘hover‘);                $(this).find(‘.address-container‘).show();            },            mouseout: function () {                $(this).removeClass(‘hover‘);                $(this).find(‘.address-container‘).hide();            }        });

$(‘#address-container‘).getCity({            province: [‘province‘, 1, 500000],            city: [‘city‘, 500000, 500100],            area: [‘area‘, 500100, 500104]        });

$(‘#address-container1‘).getCity({            province: [‘province‘],            city: [‘city‘],            area: [‘area‘]        });    })</script></body></html>

CSS代码:

.distpicker {position: relative;display: inline-block;line-height: 1.5;}

.triangle:after {content: ‘‘;border: 5px solid transparent;display: inline-block;position: absolute;right: 10%;top: 40%;width: 0;height: 0;border-top-color: #666666;}.address-content .address-tab:after, .address-content .province-list:after, .address-content .city-list:after, .address-content .area-list:after {  content: ‘‘;  display: table;  clear: both;  }

.address-title {display: inline-block;margin-left: 3px;border: 1px solid #DDD;font-size: 12px;height: 25px;line-height: 24px;position: relative;top: 1px;padding: 0 40px 0 20px;background-color: #fff;cursor: pointer;}.address-title:hover,.address-title.hover {background-color: #225f8c;color: #ffffff;}.triangle:hover:after,.address-title.hove:after {border-top-color: transparent;border-bottom-color: #ffffff;top: 3px;}

.address-container {  width: 400px;  position: absolute;  z-index: 1000;  left: 2px;  top: 30px;  background-color: #fff;  border: 1px solid #dddddd;  -moz-box-shadow: 2px 2px 5px #dddddd;  -webkit-box-shadow: 2px 2px 5px #dddddd;  box-shadow: 2px 2px 5px #dddddd;  }.address-content .address-tab { width: 100%; border-bottom: 1px solid #225f8c;  background-color: #f3f3f3;}.address-content .address-tab a {float: left;  padding: 6px 20px;  cursor: pointer;  font-size: 16px;  }.address-content .address-tab .current { background-color: #225f8c; color: #ffffff; }

.address-content .province-list, .address-content .city-list, .address-content .area-list { padding: 10px;  }.address-content .city-list, .address-content .area-list { display: none;}.address-content .province-list a, .address-content .city-list a, .address-content .area-list a {  display: inline-block;  cursor: pointer;  margin-bottom: 15px;  font-size: 14px; }.address-content .city-list a, .address-content .area-list a { padding: 4px 10px;;  margin-right: 20px; }.address-content .province-list a {  width: 20%;  text-align: center;  height: 30px;  line-height: 29px;  font-size: 14px;  }.address-content .province-list a:hover, .address-content .province-list a.active, .address-content .city-list a:hover, .address-content .city-list a.active, .address-content .area-list a:hover, .address-content .area-list a.active {  background-color: #5bc0de;  color: #ffffff;  } 
JS代码:
/**
 * Created by Administrator on 2016/11/9 0009.
 */
(function ($) {
    $.fn.getCity = function (options) {
        var settings = $.extend({
            province: null,
            city: null,
            area: null
        }, options);
        var container = this,
            province = settings.province,
            city = settings.city,
            area = settings.area,
            containerHtml = ‘‘,
            addressTab,
            provinceListA,
            cityListA,
            areaListA,
            tlt = $(container).siblings(‘.address-title‘),
            addressTabProvinceList,
            addressTabCityList,
            addressTabAreaList;
        containerHtml = ‘<input class="input-province" type="hidden" name="province" value/><input class="input-city" type="hidden" name="city" value/><input class="input-area" type="hidden" name="county" value/><div class="address-content"><div class="address-tab"><a class="current" data-id="province-list" data-cid=""></a><a data-id="city-list" data-cid=""></a><a data-id="area-list" data-cid=""></a></div><div class="province-list" data-level="2"></div><div class="city-list" data-level="3"></div><div class="area-list" data-level="4"></div></div>‘;
        $(container).html(containerHtml);
        var getCityCascade = function (type, parents, vals) {
            var html = ‘‘, parent, value, tag, addressTabA, firstElemA;
            value = (typeof (vals) == ‘undefined‘) ? 0 : vals;
            tag = $(container).find($(‘.‘ + type + ‘-list‘));
            addressTab = tag.siblings(‘.address-tab‘);
            addressTabA = addressTab.find(‘[data-id="‘ + type + ‘-list"]‘);
            if (type == ‘province‘ && typeof (parents) == ‘undefined‘) {
                parent = 1;
            } else if (type == ‘city‘ && typeof (parents) == ‘undefined‘) {
                parent = 110000
            } else if (type == ‘area‘ && typeof (parents) == ‘undefined‘) {
                parent = 110100
            } else {
                parent = parents
            }

            for (var i = 0, data = areas[type]; i < data.length; i++) {
                if (parent == data[i].area_parent) {
                    html += ‘<a "data-index="‘ + i + ‘" data-type="‘ + data[i].area_type + ‘" data-parent="‘ + data[i].area_parent + ‘" data-sn="‘ + data[i].area_sn + ‘" data-name="‘ + data[i].area_name + ‘"‘;
                    if (value == data[i].area_sn) {
                        html += ‘ class="active"‘;
                        addressTabA.html(data[i].area_name);
                        $(container).find(‘.input-‘ + type).val(value);
                    }
                    html += ‘>‘ + data[i].area_name + ‘</a>‘;
                }
            }
            tag.html(html);
            firstElemA = tag.find(‘a‘).first();
            if (value == 0) {
                addressTabA.html(firstElemA.html());
                firstElemA.addClass(‘active‘);
                $(container).find(‘.input-‘ + type).val(firstElemA.attr(‘data-sn‘));
            }
        };

        if (typeof (province) == ‘object‘) {
            getCityCascade(province[0], province[1], province[2]);
        }
        if (typeof (city) == ‘object‘) {
            getCityCascade(city[0], city[1], city[2]);
        }
        if (typeof (area) == ‘object‘) {
            getCityCascade(area[0], area[1], area[2]);
        }
        provinceListA = $(container).find($(‘.province-list‘));
        cityListA = $(container).find($(‘.city-list‘));
        areaListA = $(container).find($(‘.area-list‘));

        var classoPeration = function (tag, className) {
            tag.addClass(className).siblings(‘a‘).removeClass(className);
        };

        addressTab.on(‘click‘, ‘a‘, function () {
            classoPeration($(this), ‘current‘);
            var tag = $(this).parent();
            var elem = tag.siblings(‘.‘ + $(this).attr(‘data-id‘));
            tag.siblings().not(elem).hide();
            elem.show();
        });
        addressTabProvinceList = addressTab.find(‘[data-id="province-list"]‘);
        addressTabCityList = addressTab.find(‘[data-id="city-list"]‘);
        addressTabAreaList = addressTab.find(‘[data-id="area-list"]‘);

        provinceListA.on(‘click‘, ‘a‘, function () {
            classoPeration($(this), ‘active‘);
            getCityCascade(‘city‘, $(this).attr(‘data-sn‘));
            getCityCascade(‘area‘, cityListA.find(‘a‘).first().attr(‘data-sn‘));
            addressTabProvinceList.html($(this).html());
            $(container).find(‘.input-province‘).val($(this).attr(‘data-sn‘));
            tlt.html($(this).html());
        });
        cityListA.on(‘click‘, ‘a‘, function () {
            classoPeration($(this), ‘active‘);
            getCityCascade(‘area‘, $(this).attr(‘data-sn‘));
            addressTabCityList.html($(this).html());
            $(container).find(‘.input-city‘).val($(this).attr(‘data-sn‘));
            tlt.html(addressTabProvinceList.html() + ‘|‘ + $(this).html());
        });
        areaListA.on(‘click‘, ‘a‘, function () {
            classoPeration($(this), ‘active‘);
            addressTabAreaList.html($(this).html());
            $(container).find(‘.input-area‘).val($(this).attr(‘data-sn‘));
            tlt.html(addressTabProvinceList.html() + ‘|‘ + addressTabCityList.html() + ‘|‘ + $(this).html());
        });
    };
})(jQuery);
				
时间: 2024-12-17 17:29:29

城市选择(仿jd)的相关文章

基于jQuery仿去哪儿城市选择代码

基于jQuery仿去哪儿城市选择代码.这是一款使用的jQuery城市选择特效代码下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="lineSearch"> <ul> <li class="thRelative" id="hhDrop00"> <div class="boxSearch"> <span class="k

仿拉手团购App2--当前城市选择Activity

首页中点击城市TextView调转到当前城市选择Activity fragment_city.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background=&quo

类似于铁道部12306的城市选择框的实现

第一次写,有点小紧张... 这两天研究铁道部的余票查询系统,参考网上大牛们的经典案例,也有了一些自己的心得,写在自己程序猿的道路上记录一下,也和大家一起分享,写的不好莫怪,大牛可以自动过滤,非喜勿喷,谢谢~ 今天先简单的介绍一下城市选择框的实现,与12306官网有一点差距,上图,先看看效果:      如图所示,支持拼音首字母查询,全拼音查询,汉字查询等 好了,现在谈一谈我是怎么实现的 首先是准备工作: 我们需要把城市的信息存入我们的数据库中,城市数据来源:https://kyfw.12306.

UIPickerView的简单应用——省份/城市选择的实现

UIPickerView的简单应用--省份/城市选择的实现 实现效果如图,左边为省份选择,右边选择省份对应的城市 数据plist形式如图 工程下载地址:工程下载 https://github.com/Nongchaozhe/UIPickerView-Province-city UIPickerView的实现重要还是两个代理协议中方法的实现 - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; _pickView = [[UIPic

UIPickerView简单应用——省份/城市选择实现

UIPickerView的简单应用--省份/城市选择的实现 实现效果如图,左边为省份选择,右边选择省份对应的城市 数据plist形式如图 工程下载地址:工程下载 https://github.com/Nongchaozhe/UIPickerView-Province-city UIPickerView的实现重要还是两个代理协议中方法的实现 - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; _pickView = [[UIPic

联动城市选择插件

index.html <!DOCTYPE html><html lang="en"> <head>    <meta charset="UTF-8">    <title>kuCity</title>    <link rel="stylesheet" href="kuCity.css">    <style>    .search

基于jquery的城市选择插件

城市选择插件的难度不是很大,主要是对dom节点的操作.而我写的这个插件相对功能比较简答,没有加入省市联动. 上代码好了,参照代码的注释应该比较好理解. 1 /* 2 *基于jquery的城市选择插件 3 *author:youziclub 4 *2015-4-22 5 */ 6 ;(function($){ 7 $.fn.city=function(options){ 8 // 城市信息 9 var nav=['热门','A-G','H-L','M-T','W-Z','其他']; 10 var

移动端城市选择JavaScript插件(基于WG的城市选择插件的修改版本)

周末的时候趁着一次机会,拿WG(博客)开发的城市选择插件改了一个移动端可以直接用的城市选择插件. 原版插件是基于原声JavaScript写的,在此先感谢作者. 我做的只是依照肯德基注册会员的页面的交互效果改了一下界面,同时将各个触发效果改成了跟肯德基注册页面类似的交互效果,源程序不依赖jQuery但是我自己做简单的交互的时候偷懒使用了jQuery所以,如果您要使用这个插件完全可以换掉我写的那一些事件注册. 详细的因为我并没有大改,所以也就不po细节了,具体的项目代码我贴在这里. https://

多图选择(仿微信,陌陌)

整理了下网上的资源,并做了大量修改,美化,完成以下效果,仿微信陌陌多图选择,最多可以选择6张图片,选择完成后,用GridView展示,点击图片可以删除,其实,以此为基础还可以做出更多更炫的效果,不过本代码仅供参考,若有需要,自己可以进行扩展,接下来看效果图: 源码下载地址:http://download.csdn.net/detail/baiyuliang2013/7749241 多图选择(仿微信,陌陌)

android wheelview实现三级城市选择

很早之前看淘宝就有了ios那种的城市选择控件,当时也看到网友有分享,不过那个写的很烂,后来(大概是去年吧),我们公司有这么一个项目,当时用的还是网上比较流行的那个黑框的那个,感觉特别的丑,然后我在那个开源的wheelview的基础上做封装,用户只需要专心数据的组装即可,然后填充就行,其他的可以不必考虑. 先上下效果图 接下来说下我的思路:网络请求-数据返回-设置数据-数据填充控件 接下来直接按上面的流程直接上代码: 网络请求我用的本地的json数据 String address = Utils.