DIV CSS 佈局教程網

原生 select 打造手機版省/市/區聯動選擇
編輯:關於JavaScript     

最近在處理一批表單類的行動版頁面,遇到省市區聯動選擇的需求。原本想直接沿用公用庫裡PC 端的元件,實際跑在手機上效果實在不理想,而設計也沒給出明確的視覺稿,最後只好自己用原生 select 重寫了一版。樣式直接採用手機系統的選單外觀,勉強堪用;資料沿用 PC 端的既有資料集,只是把元件邏輯整份改寫。

 var $ = require('jquery'), $window = $(window), data = require('./data-new'), $doc = $(document); var __DEFAULTS__ = { wrap:'', itemName: ['省', '市', '區/縣'], items: ['province', 'city', 'area'], callback: function(field, index) {} // 點擊切換得時候才會執行 }; function Area(options){ options = $.extend({}, __DEFAULTS__, options); var that = this; that.wrapper = $(options.wrap); that.selectArr = that.wrapper.data('default')?that.wrapper.data('default').split(','):[110000,110100,110101]; // that.items = options.items; that.itemName = options.itemName; that.callback = options.callback; that.setValue(); that.events(); that.default = that.wrapper.data('default'); //默認輸出省市區的id that.validinput = $("#default-area"); var validval = that.default!==undefined?that.default:''; that.validinput.val(validval); } Area.prototype = { constructor: Area, //創建select,輸出相應的數據 createItems:function(itemname,data,selectId){ var that = this; //如果默認的defalut值沒輸出,則默認的給select添加相應的填寫提示 var html = '<select name="'+itemname+'">'+(that.default === undefined ?'<option value="'+itemname+'" selected ="selected">'+that.itemName[that.index]+'</option> ' : ''); for (var k in data) { html += '<option value ="'+ data[k].id +'"'+(selectId === data[k].id ? 'selected = "selected"' : '')+'>' + data[k].name + '</option>'; } html += '</select>'; return html; }, //設置初始值 setValue:function(){ var that = this, html = ''; $.each(that.selectArr,function(index,k){ that.index = index; html += that.createItems(that.items[index],that.getData(that.items[index],that.selectArr[index-1]),k); }) that.wrapper.append(html) }, //獲取數據 getData: function(type, pid) { if (type === 'province') { return data.provinces || []; // 省份信息不需要pid } if (type === 'city') { return data.cities[pid] || []; } if (type === 'area') { return data.areas[pid] || []; } }, //獲取select索引值 getItemIndex:function(type){ var that = this; for(var i= 0,l = that.items.length;i<l;i++){ if(that.items[i] == type){ return i; } } }, //觸發change時候,select下一位值重新初始化 setItemVal:function(select){ var that = this; var $this = select, previd = $this.val(), $type =$this.attr('name'), $nxtType = ''; if($type!='area'){ $nxtType = that.items[that.getItemIndex($type)+1]; var data = that.getData($nxtType,previd), html = that.createItems($nxtType,data,previd), nextSelect = $('select[name="'+$nxtType+'"]'); if($this.siblings('select[name="'+$nxtType+'"]').length>0){ nextSelect.remove(); } $this.after(html); nextSelect.find('option:first').prop('selected',true); $('select[name="'+$nxtType+'"]').trigger('change'); } else{ that.validinput.val($this.val()).trigger('validate') } that.index = that.getItemIndex($type); //觸發change後可以設置回調函數 if (that.callback) { that.callback.call(this, select, that.getItemIndex($type)); } }, events:function(){ var that = this; //select change事件 $doc.on('change','.area-container select',function(){ that.setItemVal($(this)); }) } } module.exports = Area; 

html 部分只需要一個隱藏欄位與一個容器:

 <input type="hidden" name="defaultArea" value="" id="default-area" > <div class="area-container" data-default=""></div>

隱藏欄位 #default-area 是必填的,用來承載最終選出的行政區 ID 並觸發驗證;data-default 則可傳入「110000,110100,110101」這樣的三段 ID 作為初始值。

幾個設計細節:聯動的核心在 setItemVal,選擇上一層後就移除並重建下一層 select,再觸發它的 change 事件形成連鎖更新;getItemIndex 負責在 items 陣列裡定位目前是哪一層,藉此判斷有沒有「下一層」;最末端的 area 不再重建子選單,而是把值寫進隱藏欄位並觸發 validate,剛好接上既有的表單驗證流程。整個元件用原型方法組織,對外只暴露一個建構子,在 React/Vue 這類環境中要移植也不算太難。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved