html级联选择器,jquery实现 级联选择器

看到UI在做设计图 有级联选择器 就先写写 (项目还在用jquery~~~~~~)

先上效果图

4bb7231739f02bb805de42a5b4d928f9.png

css

.box{

position: relative;

width: 800px;

height: 800px;

background: #2A363E;

}

.input{

font-size: 20px;

color: #fff;

}

.input>input{

width: 500px;

height: 50px;

font-size: 20px;

color: #fff;

border: 2px solid aqua;

background: #2A363E;

padding:0 10px;

}

.select{

position: absolute;

top: 70px;

left: 475px;

}

.select>ul{

background: #0A1824;

color: #474F55;

width: 140px;

padding: 10px 0;

}

.select>ul::before

{

content: "";

position: relative;

display:inline-block;

top: -23px;

left:90px;

border-bottom: 10px solid #0A1824;

border-right: 10px solid transparent;

border-left: 10px solid transparent

}

.select>ul>li{

position: relative;

}

.select>ul>li>p{

line-height: 30px;

text-align: center;

}

.select>ul>li>ul{

position: absolute;

background: #0A1824;

color: #474F55;

width: 140px;

padding: 10px 0;

left: 155px;

top: -5px;

display:none

}

.select>ul>li>ul::before

{

content: "";

position: absolute;

display:inline-block;

top: 10px;

left:-10px;

border-bottom: 10px solid transparent;

border-top: 10px solid transparent;

border-right: 10px solid #0A1824;

}

.select>ul>li>ul>li{

line-height: 30px;

text-align: center;

}

.select>ul>li>ul>li:hover{

color: aqua;

background: #474F55;

}

.select>ul>li>p:hover{

color: aqua;

background: #474F55;

}

html

巡查点位:

变电东区1

东1号位

东2号位

东3号位

东4号位

变电西区2

西1号位

西2号位

西3号位

西4号位

变电南区3

南1号位

南2号位

南3号位

南4号位

变电北区4

北1号位

北2号位

北3号位

北4号位

js

var val = ''

$('.input').on('click', () => {

$('.select>ul').show()

})

$('.select>ul>li>p').on('click', function () {

$(this).css({

color: 'aqua',

background: '#474F55'

}).parent('li').siblings().children('p').css({

color: '#474F55',

background: '#0A1824'

})

val = $(this).text()

$('.input>input').val(val)

$(this).siblings('ul').show()

$(this).parent('li').siblings().children('ul').hide()

})

$('.select>ul>li>ul>li').on('click', function () {

$(this).css({

color: 'aqua',

background: '#474F55'

}).siblings().css({

color: '#474F55',

background: '#0A1824'

})

$('.input>input').val(val + '/' + $(this).text())

$(this).css({

color: '#474F55',

background: '#0A1824'

})

$('.select>ul>li>p').css({

color: '#474F55',

background: '#0A1824'

})

$('.select>ul').hide()

})

注: 真正的使用不要像一样 >>*>没完 还有.parent().siblings().children() 这样没完 我只是写个小dome 真正的写的时候 我会多起几个类名;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML级联选择器是一种常见的表单控件,用于让用户在多个选项中进行选择。使用selectize.js可以方便地实现自定义的级联选择器,使得用户可以根据前一个选项的选择动态地加载后续选项。 以下是一个基于selectize.js实现的简单级联选择器: ```html <!DOCTYPE html> <html> <head> <title>级联选择器</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" integrity="sha256-Z6TzH4WbQYZSj8VvD6g5cFw7fRf/3jz6OwvYbK2e5pw=" crossorigin="anonymous" /> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha384-ZvpUoO/+PpLXR1lu4jmpXWu80pZlYUAfxl5NsBMWOEPSjUn/6Z/hRTt8+pR6L4N2" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.min.js" integrity="sha256-8iKwBtOJU/8FJjN9ryftmrgyG3zIiVp8c7K4Zn0QGB0=" crossorigin="anonymous"></script> </head> <body> <label for="country">国家:</label> <select id="country" name="country" class="selectized"> <option value="">请选择国家</option> <option value="china">中国</option> <option value="usa">美国</option> </select> <label for="city">城市:</label> <select id="city" name="city" class="selectized"> <option value="">请选择城市</option> </select> <script> $(document).ready(function() { var cities = { china: ['北京', '上海', '广州', '深圳'], usa: ['纽约', '洛杉矶', '芝加哥', '旧金山'] }; $('#country').selectize({ onChange: function(value) { if (!value.length) return; var citySelect = $('#city').selectize()[0].selectize; citySelect.clearOptions(); citySelect.load(function(callback) { callback(cities[value]); }); } }); $('#city').selectize(); }); </script> </body> </html> ``` 在上面的代码中,我们定义了两个select元素,一个是国家选项,一个是城市选项。在国家选项中选择一个选项后,城市选项会根据所选国家动态加载对应的城市选项。 实现的关键在于通过selectize.js的API来实现级联选择器的动态加载。在国家选项的onChange事件中,我们通过load方法来动态加载城市选项。这个方法会接受一个回调函数作为参数,这个回调函数用于返回后续选项的选项值数组。在这个例子中,我们根据所选国家返回相应的城市数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值