npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

linkage-selector

v1.0.0

Published

JavaScript 多级联动下拉菜单选择器

Downloads

16

Readme

LinkageSelector

JavaScript 多级联动下拉菜单选择器

演示

http://cople.github.io/LinkageSelector

安装

CDN

<script src="https://unpkg.com/linkage-selector/dist/linkage-selector.min.js"></script>
<!-- or jQuery Plugin -->
<script src="https://unpkg.com/linkage-selector/dist/jquery.linkage-selector.min.js"></script>

NPM

npm install linkage-selector --save

使用

<form id="demo1">
    <select><option>加载中</option></select>
    <select><option>加载中</option></select>
</form>

<form id="demo2">
    <!-- 也可以自动生成 select 元素 -->
</form>
var selector1 = new LinkageSelector({
    container: '#demo1',
    data: data
});

var selector2 = new LinkageSelector({
    container: '#demo2',
    dataUrl: 'data.json',
    placeholder: ['省', '市', '区'],
    defaultValue: ['海南省', '三沙市', '西沙群岛'],
    onRender: function(els) {
        [].forEach.call(els, function(el) {
            el.className = 'form-control';
        });
    },
    onChange: function(value) {
    	console.log(`省:${value[0]},市:${value[1]},区:${value[2]}。`);
    }
});

// With jQuery
// $('#demo1').linkageSelector(options);

选项

| 参数 | 类型 | 默认值 | 描述 | | ------------ | ---------------------- | ---------------------------------------- | ---------------------------------------- | | data | object | {} | 详见数据结构 | | dataUrl | string | null | 远程数据接口地址 | | container | string|element | document.body | 容器 | | selector | string | 'select' | select 元素的选择器 | | placeholder | string|array|boolean | '请选择' | 下拉框的第一项,统一设置用 string ,分别设置用 array,不添加则为 false。(单个字符串格式可以为 名称|值) | | defaultValue | array | null | 默认值 | | dataKeys | object | { name: 'name', value: 'code', children: 'childs' } | 数据字段的键名 | | required | boolean | true | 是否添加 required 属性 | | emptyStyle | string | 'disabled' | 下拉框为空时的处理方法:disabled(添加 disabled 属性) / hidden(隐藏元素) / none | | onRender | function | | 生成 select 元素时触发,参数为元素数组 | | onChange | function | | 下拉框值变化时触发,参数为值数组 |

方法

| 名称 | 返回值 | 描述 | | --------------------- | ----- | ------ | | setValue(value:array) | | 设置下拉框值 | | getValue() | array | 获取值数组 | | resetValue() | | 重置下拉框 |

数据结构

[{
    "code": "11",
    "name": "北京市",
    "childs": [{
        "code": "1101",
        "name": "市辖区",
        "childs": [
            { "code": "110101", "name": "东城区" },
            { "code": "110102", "name": "西城区" },
            { "code": "110105", "name": "朝阳区" }]
    }]
}, {
    "code": "31",
    "name": "上海市",
    "childs": [{
        "code": "3101",
        "name": "市辖区",
        "childs": [
            { "code": "310101", "name": "黄浦区" },
            { "code": "310104", "name": "徐汇区" },
            { "code": "310105", "name": "长宁区" }]
    }]
}, {
    "code": "44",
    "name": "广东省",
    "childs": [{
        "code": "4401",
        "name": "广州市",
        "childs": [
            { "code": "440103", "name": "荔湾区" },
            { "code": "440104", "name": "越秀区" },
            { "code": "440105", "name": "海珠区" }]
    }]
}]

如果名称和值相同,也可以用紧凑型的 Object 对象:

{
    "北京市": {
        "市辖区": ["东城区", "西城区", "朝阳区"]
    },
    "上海市": {
        "市辖区": ["黄浦区", "徐汇区", "长宁区"]
    },
    "广东省": {
        "广州市": ["荔湾区", "越秀区", "海珠区"]
    }
}

License

MIT