js、jquery实现省市县三级联动下拉框的功能

更新日期: 2018-08-07阅读: 3.4k标签: jquery

这篇文章主要为大家详细介绍使用原生js、或jquery来实现省市县三级联动下拉框的功能。具有一定的参考价值,感兴趣的小伙伴们可以参考一下


原生js的实现

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
</head>
<body>
<select>
    <option>---请选择省---</option>
</select>
<select>
    <option>---请选择市---</option>
</select>
<select>
    <option>请选择县</option>
</select>

<script type="text/javascript">
    var proArr = ["安徽","河南","河北","江苏"];
    var cityArr = [
        ["合肥","蚌埠","亳州","阜阳"],
        ["郑州","开封","商丘","焦作"],
        ["石家庄","邯郸","衡水"],
        ["苏州","杭州","无锡","南京"]
    ]
    var couArr = [
        [
            ["合县1","合县2","合县3","合县4"],
            ["蚌县1","蚌县2","蚌县3","蚌县4"],
            ["亳县1","亳县2","亳县3","亳县4"],
            ["阜县1","阜县2","阜县3","阜县4"],
        ],
        [
            ["郑县1","郑县2","郑县3","郑县4"],
            ["开县1","开县2","开县3","开县4"],
            ["商县1","商县2","商县3","商县4"],
            ["焦县1","焦县2","焦县3","焦县4"],
        ],
        [
            ["石县1","石县2","石县3","石县4"],
            ["邯县1","邯县2","邯县3","邯县4"],
            ["衡县1","衡县2","衡县3","衡县4"],
        ],
        [
            ["苏县1","苏县2","苏县3","苏县4"],
            ["杭县1","杭县2","杭县3","杭县4"],
            ["无县1","无县2","无县3","无县4"],
            ["南县1","南县2","南县3","南县4"],
        ],
    ]
    var sltNope = document.getElementsByTagName("select");
    var Index1 = 0;
    createNope(proArr,0);
    sltNope[0].onchange = function(){
        sltNope[1].length = 1;
        sltNope[2].length = 1;
        Index = this.selectedIndex-1;
        createNope(cityArr[Index],1);
    }
    sltNope[1].onchange = function(){
        sltNope[2].length = 1;
        createNope(couArr[Index][this.selectedIndex-1],2);
    }    
    function createNope(Arr,sum){
        for (x = 0;x<Arr.length;x++) {
            var topy = document.createElement('option');
            topy.innerHTML = Arr[x];
            sltNope[sum].appendChild(topy);
        }
    }
</script>
</body>
</html>


jquery的实现

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery-3.1.1.js"></script>
</head>
<body>
<select>
    <option>---请选择省---</option>
</select>
<select>
    <option>---请选择市---</option>
</select>
<select>
    <option>请选择县</option>
</select>

<script type="text/javascript">
    var proArr = ["安徽","河南","河北","江苏"];
    var cityArr = [
        ["合肥","蚌埠","亳州","阜阳"],
        ["郑州","开封","商丘","焦作"],
        ["石家庄","邯郸","衡水"],
        ["苏州","杭州","无锡","南京"]
    ]
    var couArr = [
        [
            ["合县1","合县2","合县3","合县4"],
            ["蚌县1","蚌县2","蚌县3","蚌县4"],
            ["亳县1","亳县2","亳县3","亳县4"],
            ["阜县1","阜县2","阜县3","阜县4"],
        ],
        [
            ["郑县1","郑县2","郑县3","郑县4"],
            ["开县1","开县2","开县3","开县4"],
            ["商县1","商县2","商县3","商县4"],
            ["焦县1","焦县2","焦县3","焦县4"],
        ],
        [
            ["石县1","石县2","石县3","石县4"],
            ["邯县1","邯县2","邯县3","邯县4"],
            ["衡县1","衡县2","衡县3","衡县4"],
        ],
        [
            ["苏县1","苏县2","苏县3","苏县4"],
            ["杭县1","杭县2","杭县3","杭县4"],
            ["无县1","无县2","无县3","无县4"],
            ["南县1","南县2","南县3","南县4"],
        ],
    ]
    createNope(proArr,0);
    $("select")[0].onchange = function(){
        $("select")[1].length = 1;
        $("select")[2].length = 1;
        Index = this.selectedIndex-1;
        createNope(cityArr[Index],1);
    }
    $("select")[1].onchange = function(){
        $("select")[2].length = 1;
        createNope(couArr[Index][this.selectedIndex-1],2);
    }
    function createNope(Arr,sum){
        $.each(Arr,function(index,value){
            $("select").eq(sum).append("<option>"+value+"</option>");
        })
    }
</script>
</body>
</html>


jquery的插件实现:

distpicker是一款可以实现中国省市区地址三级联动jQuery插件。它使用简单,简单设置即可完成中国省市区地址联动效果。


链接: https://fly63.com/article/detial/988

是时候和 jQuery 说拜拜了么?

在网络上也时不时会看到,“是时候和jQuery说拜拜了”,最著名的莫过于在2013年的这篇文章You Might Not Need jQuery。

前端程序员应该知道的15个jQuery小技巧

15个jQuery小技巧:返回顶部按钮,预加载图像,检查图像是否加载,自动修复破坏的图像,悬停切换类,禁用输入字段,停止加载链接,淡入/滑动切换,简单的手风琴...

Jquery插件开发的方法总汇

jquery插件是用来扩展jquery对象的一种方法,它的使用方法是通过jquery对象$来调用。其中Jquery插件开发一共有三种方式:$.extend(),$.fn,$.widget()

JQuery 自动触发事件

在JQuery中,可以使用trigger()方法完成模拟操作,trigger()方法不仅能触发浏览器支持的具有相同名称的事件,也可以触发自定义名称的事件。rigger(type[,data])方法有两个参数

高版本jquery中attr和prop的区别

在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。

原生js中DOM对象转成jQuery对象、jQuery 对象转成 DOM 对象的实现

jquery是对js语言的封装、扩展,实现了对浏览器的兼容,使用jquery能让操作更方便简洁,这篇文章主要讲解原生js中Dom对象和jquery对象的相互转换。

jquery分页插件pagination.js报错pagination is not a function的bug解决方法

在使用jquery.pagination.js插件的时候,会出现pagination is not a function的错误,这是什么原因导致的呢?这里为大家整理一下,请对比自己的代码参考!

jQuery 常用小技巧分享

整理一些简单技巧的集合,帮你提升 jQuery 技能,你可以直接拿来使用,下面内容包括:禁止右键点击、隐藏搜索文本框文字、隐藏搜索文本框文字、在新窗口中打开链接、检测浏览器...

jquery动画实现_浅谈jQuery之动画

jQuery提供了JS未能提供的动画效果,利用jQuery的动画效果,可以极大的简化JS动画部分的逻辑,包括:滑入滑出动画、淡入淡出动画、显示隐藏动画、停止动画、自定义动画

jQuery如何为指定标签添加和删除一个样式

在网页的实际应用中,需要根据不同的条件来改变元素的CSS样式,通过动态的给元素添加删除一个CSS类可以实现此功能,下面通过实例来介绍一下如何实现此种功能。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!