移动端的城市-区县联动
本站之前已经发布过类似的代码,每个展示方式都不是同的,可以试试搜索联动查看全部相关代码
XML/HTML Code
- <form class="store-info-form info-form-page">
- <div class="li-title">
- <h3>城市-区县联动</h3>
- </div>
- <div class="form-item">
- <label class="input-group">
- 城市 : <span class="n-msg n-msg-city required-tip">(必填项)</span>
- </label>
- <div class="controls controls-store controls-city">
- <div class="city-wrapper">
- <input type="text" id="city" name="city" class="current-city" readonly placeholder="请选择">
- <i class="icon-down"></i>
- </div>
- <div class="area-wrapper">
- <input type="text" id="district" name="district" class="current-area" readonly placeholder="请选择">
- <i class="icon-down"></i>
- </div>
- </div>
- </div>
- <div class="masker hidden"></div>
- <div class="city-box hidden">
- <section class="express-city-box">
- <div class="hot-city-title">
- <i></i>
- <h4>热门城市</h4>
- </div>
- <div id="city-sidaber" class="city-sidaber">
- <p>A</p><p>B</p><p>C</p><p>D</p><p>E</p><p>F</p><p>G</p><p>H</p><p>J</p><p>K</p><p>L</p><p>M</p><p>N</p><p>P</p><p>Q</p><p>R</p><p>S</p><p>T</p><p>W</p><p>X</p><p>Y</p><p>Z</p>
- </div>
- <div class="hot-city">
- <div class="hot">
- <a data-value="北京市">北京</a>
- <a data-value="上海市">上海</a>
- <a data-value="南京市">南京</a>
- <a data-value="广州市">广州</a>
- </div>
- </div>
- <article id="cityBox" class="wrapper">
- <section class="content">
- </section>
- </article>
- </section>
- </div>
- <div class="area-box hidden">
- <div class="hot-city-title">
- <i></i>
- <h4>区/县</h4>
- </div>
- <ul class="citylist">
- <li class="cityitem" values="朝阳区">朝阳区</li>
- </ul>
- </div>
- </form>
JavaScript Code
- <script>
- var options = {
- scrollY: true, // 因为scrollY默认为true,其实可以省略
- scrollX: true,
- mouseWheel: true,
- click: true
- }
- var wrapper = document.querySelector('.wrapper');
- var scroll = new BScroll(wrapper, options);
- // 城市选择
- $('input[name="city"]').on('click',function(){
- $('.city-box').slideDown(500,function(){
- scroll.refresh();
- });
- $('.masker').removeClass('hidden');
- })
- $('.masker').on('click', function(){
- $('.masker').addClass('hidden');
- $('.city-box').slideUp(500);
- $('.area-box').slideUp(500);
- })
- $('.city-box .content').on('click', 'li', function(e){
- $('input[name="city"]').val(e.target.innerHTML);
- $('input[name="district"]').val('');
- $('.city-box').slideUp(500);
- $('.masker').addClass('hidden');
- })
- $('.hot-city').on('click', 'a', function(e){
- $(this).addClass('active');
- var hotValue = $(this).attr('data-value');
- $('input[name="city"]').val(hotValue);
- $('input[name="district"]').val('');
- $('.city-box').slideUp(500, function(){
- $('.hot-city a').removeClass('active');
- });
- $('.masker').addClass('hidden');
- })
- $('.city-sidaber').on('click',function(e){
- var value = e.target.innerHTML;
- var alphabet = $('.letter-title');
- alphabet.map(function(i,item){
- if(item.innerHTML == value){
- scroll.scrollToElement(item);
- }
- })
- })
- $('input[name="district"]').on('click',function(){
- var preValue = $('input[name="city"]').val();
- if(preValue){
- traverseArea(preValue);
- $('.area-box').slideDown(500);
- $('.masker').removeClass('hidden');
- }
- })
- $('.citylist').on('click', 'li', function(e){
- $('input[name="district"]').val(e.target.innerHTML);
- $('.n-msg-city').removeClass('tip-active').text("(必填项)");
- $('.area-box').slideUp(500);
- $('.masker').addClass('hidden');
- })
- </script>
原文地址:http://www.freejs.net/article_biaodan_767.html