Giter Club home page Giter Club logo

distpicker's Introduction

Distpicker

Downloads Version Gzip Size

A simple jQuery plugin for picking provinces, cities and districts of China.

请注意以下市/县并未设置下一级的区:济源市、潜江市、神农架林区、天门市、仙桃市、东莞市、中山市、东沙群岛、白沙黎族自治县、保亭黎族苗族自治县、昌江黎族自治县、澄迈县、儋州市、定安县、东方市、乐东黎族自治县、临高县、陵水黎族自治县、琼海市、琼中黎族苗族自治县、屯昌县、万宁市、文昌市、五指山市、嘉峪关市、阿拉尔市、北屯市、可克达拉市、昆玉市、石河子市、双河市、铁门关市、图木舒克市、五家渠市。

Table of contents

Main files

dist/
├── distpicker.js        (UMD)
├── distpicker.min.js    (UMD, compressed)
├── distpicker.common.js (CommonJS, default)
└── distpicker.esm.js    (ES Module)

Getting started

Install

npm install distpicker

Include files:

<script src="/path/to/jquery.js"></script><!-- jQuery is required -->
<script src="/path/to/distpicker.js"></script>

Create HTML elements:

<div><!-- container -->
  <select></select><!-- province -->
  <select></select><!-- city -->
  <select></select><!-- district -->
</div>

Usage

Initialize with data-toggle="distpicker" attribute

Basic

<div data-toggle="distpicker">
  <select></select>
  <select></select>
  <select></select>
</div>

Custom placeholders

<div data-toggle="distpicker">
  <select data-province="---- 选择省 ----"></select>
  <select data-city="---- 选择市 ----"></select>
  <select data-district="---- 选择区 ----"></select>
</div>

Custom districts

<div data-toggle="distpicker">
  <select data-province="浙江省"></select>
  <select data-city="杭州市"></select>
  <select data-district="西湖区"></select>
</div>

Initialize with $.fn.distpicker method

Basic

$('#target').distpicker();

Custom placeholders

$('#target').distpicker({
  province: '---- 所在省 ----',
  city: '---- 所在市 ----',
  district: '---- 所在区 ----'
});

Custom districts

$('#target').distpicker({
  province: '浙江省',
  city: '杭州市',
  district: '西湖区'
});

⬆ back to top

Options

  • Change the default options with $().distpicker(options).
  • Change the global default options with $.fn.distpicker.setDefaults(options).

Also supports to set the options by data-* attributes:

<div data-toggle="distpicker" data-autoselect="3" data-province="浙江省">...</div>

autoselect

  • Type: Number
  • Options:
    • 0: Disable autoselect.
    • 1: Autoselect province only.
    • 2: Autoselect province and city only.
    • 3: Autoselect all (province, city and district).
  • Default: 0

Selects the districts automatically.

placeholder

  • Type: Boolean
  • Default: true

Show placeholder (with an <option> element).

valueType

  • Type: String
  • Options:
    • 'name': administrative division name.
    • 'code': administrative division code.
  • Default: 'name'

Defines the value type of the <select> element.

Note that this option in data-* attribute should be data-value-type:

<div data-toggle="distpicker" data-value-type="code">...</div>

province

  • Type: String
  • Default: —— 省 ——

Defines the initial value of province <select>. If it is a valid province, it will be selected. If not, it will be used as a placeholder.

city

  • Type: String
  • Default: —— 市 ——

Defines the initial value of city <select>. If it is a valid city under the selected province, it will be selected. If not, it will be used as a placeholder.

district

  • Type: String
  • Default: —— 区 ——

Defines the initial value of district <select>. If it is a valid district under the selected city, it will be selected. If not, it will be used as a placeholder.

⬆ back to top

Methods

getDistricts([districtCode])

  • districtCode (optional):

    • Type: Number
    • The district code of target country, province or city.
    • If not present, will return all the districts.
  • (return value):

    • Type: Object

Get districts data.

$().distpicker('getDistricts'); // **
$().distpicker('getDistricts', 330000); // 浙江省
$().distpicker('getDistricts', 330100); // 杭州市

reset([deep])

  • deep (optional):
    • Type: Boolean
    • Default: false
    • Reset the selects to default states (Undo selected).

Reset the selects to the initial states (Undo changed).

$().distpicker('reset');
$().distpicker('reset', true);

destroy()

Destroy the distpicker instance, but keep the selected districts.

If you want to remove the selected districts, you can call reset method first and then call this method.

⬆ back to top

No conflict

If you have to use other plugin with the same namespace, just call the $.fn.distpicker.noConflict method to revert to it.

<script src="other-plugin.js"></script>
<script src="distpicker.js"></script>
<script>
  $.fn.distpicker.noConflict();
  // Code that uses other plugin's "$().distpicker" can follow here.
</script>

Browser support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Opera (latest)
  • Edge (latest)
  • Internet Explorer 9+

As a jQuery plugin, you also need to see the jQuery Browser Support.

License

MIT © Chen Fengyuan

⬆ back to top

distpicker's People

Contributors

dependabot[bot] avatar fengyuanchen avatar kennynaoh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

distpicker's Issues

二次设置selected无效

通过

var config = {


province: province,


city: city,


district: district


};

console.log(config);

$("#distpicker").distpicker(config);

第一次,选中情况没问题

第二次继续调用上述代码,导致显示的还是第一次的值

对了,可以通过中文设置选中,我用的中文选中

数据塞不进去

用js写入的的html用
$("#distpicker3").distpicker({
province: data.province,
city: data.city,
district: data.district
});
这个方法不能把获取到的数据写进去,用下面的写法,
$('select[name="province"]').val(data.result['province']);
$("#distpicker").distpicker("output","city");
$('select[name="city"]').val(data.result['city']);
$("#distpicker").distpicker("output","district");
$('select[name="district"]').val(data.result['district']);
好像是塞数据的时候,没有触发change
还是我哪里用错了呢

valueType bug

标签的属性不认驼峰模式,要么小写,要么蛇形aa-bb-cc。valueType这个属性在js中无法获取,转成小写可以;此bug影响 code 情境下的 城市输出功能以及初始化功能

autoselect需要细分

autoselect等于true的情况下,要区分是自动选择省、市、区的默认值,还是选择省后自动选择市。

有些时候我们不需要默认值,将autoselect设置为false后,选择省后,又不会自动选择市。

我想我应该描述清楚了我的问题:是自动选择默认值,还是自动选择下级区域。

数据有错误

441900: {
441900: '三元里'
},
442000: {
442000: '湖滨北路'
},

东莞市,中山市,下面没有区县哦,另外我整理了一版mysql数据,正好能跟你这个配合上,有兴趣加我聊聊

Placeholders not working

distpicker always skip placeholder "---- 选择省 ----" and
shows the next item on the list "北京“ by default.

省市区不能数字转中文

$().distpicker('getDistricts'); // **
$().distpicker('getDistricts', 330000); // 浙江省
$().distpicker('getDistricts', 330100); // 杭州市

返回一个对象 不是 字符串

没效果...

把代码里面这行删掉
this.each(function each() {
/里面的内容保留/
});

然后就可以了
$().distpicker('getDistricts')['330000']
"浙江省"
$().distpicker('getDistricts',330000)[330100]
"杭州市"
$().distpicker('getDistricts',330100)['330102']
"上城区"

解决..

我不懂JS瞎改的 大佬看看是不是这个意思...

getDistricts 不能根据code获取name

$().distpicker('getDistricts'); // **
$().distpicker('getDistricts', 330000); // 浙江省
$().distpicker('getDistricts', 330100); // 杭州市

不能根据code获取name

与vue的冲突

使用vue,通过v-model绑定数据,为什么只能显示省级栏,而市级和区级下面就没数据了 求作者帮助下!

缺少**省

hello,您的插件好像少了**省的行政区划

求问

怎么取得json里面的数据遍历出来

如何反显数据?

请问后台返回一个地址后(比如北京市海淀区,海淀区的areaCode为110108),如何用distpicker反显数据,直接显示海淀区呢?

海南省行政区缺失

三亚市下辖地区:吉阳区、崖州区、天涯区、海棠区。
现在的资源文件里没有。
请问资源文件是否考虑近期更新?

谢谢!

疑问1

这个没办法获取到select 选择框里面的城市信息 比如我想弹出 北京---东城区

custom district

I just need to replace district data to add new country district?

省市县初始赋值能不能是对应的ID

如初始赋值是北京市,对应的ID是100000,如何赋值100000,显示的是北京市?代码怎么写?
$("#distpicker3").distpicker({
province: '浙江省',
city: '杭州市',
district: '西湖区'
});
如何写成
$("#distpicker3").distpicker({
province: '310000',
city: '310010',
district: '310011'
});

requirejs引入distpicker页面没效果

我用requirejs引入了jquery和distpicker(页面元素能查找得到两者已引入)
但是标签设置如下,并没有效果,请问是怎么回事?谢谢

<div data-toggle="distpicker">
  <select data-province="浙江省"></select>
  <select data-city="杭州市"></select>
  <select data-district="西湖区"></select>
</div>

没有效果

怎么用代码切换省市区

html:
<div id="distpicker3">
    <select></select>
    <select></select>
    <select></select>
</div>
<div>
    <button id="toggleDistpicker">切换</button>
</div>


js:
$('#distpicker3').distpicker({
    province: '浙江省',
    city: '杭州市',
    district: '西湖区'
});

$('#toggleDistpicker').click(function(){
    $('#distpicker3').distpicker({
        province: '河北省',
        city: '保定市',
        district: '竞秀区'
    });
});

这样点击时没有作用,请问如何改变3个值

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.