js select實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)選擇
最近整了一一些表單類的移動(dòng)頁(yè)面,遇到了一個(gè)省市區(qū)的聯(lián)動(dòng)選擇,本來(lái)想著用公用庫(kù)里面的以前pc端的省市區(qū)選擇組件,但是發(fā)現(xiàn)pc端的效果在手機(jī)端用效果太不理想,設(shè)計(jì)沒(méi)給出具體的設(shè)計(jì)效果,只好自己整了select原生的省市區(qū)選擇效果,樣式使用手機(jī)自帶的效果,感覺(jué)樣式效果湊合還能用,數(shù)據(jù)還是用的pc的數(shù)據(jù),只是把組件的給重寫(xiě)了一下,代碼效果如下:
var $ = require('jquery'), $window = $(window), data = require('./data-new'), $doc = $(document); var __DEFAULTS__ = { wrap:'', itemName: ['省', '市', '區(qū)/縣'], items: ['province', 'city', 'area'], callback: function(field, index) {} // 點(diǎn)擊切換得時(shí)候才會(huì)執(zhí)行 }; 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'); //默認(rèn)輸出省市區(qū)的id that.validinput = $("#default-area"); var validval = that.default!==undefined?that.default:''; that.validinput.val(validval); } Area.prototype = { constructor: Area, //創(chuàng)建select,輸出相應(yīng)的數(shù)據(jù) createItems:function(itemname,data,selectId){ var that = this; //如果默認(rèn)的defalut值沒(méi)輸出,則默認(rèn)的給select添加相應(yīng)的填寫(xiě)提示 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; }, //設(shè)置初始值 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) }, //獲取數(shù)據(jù) 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; } } }, //觸發(fā)change時(shí)候,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); //觸發(fā)change后可以設(shè)置回調(diào)函數(shù) 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代碼:
<div class="area-container" data-default=""></div>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于javascript bootstrap實(shí)現(xiàn)生日日期聯(lián)動(dòng)選擇
- JS簡(jiǎn)單實(shí)現(xiàn)城市二級(jí)聯(lián)動(dòng)選擇插件的方法
- JSON+HTML實(shí)現(xiàn)國(guó)家省市聯(lián)動(dòng)選擇效果
- js實(shí)現(xiàn)一個(gè)省市區(qū)三級(jí)聯(lián)動(dòng)選擇框代碼分享
- javascript 日期聯(lián)動(dòng)選擇器 [其中的一些代碼值得學(xué)習(xí)]
- Js操作Select大全(取值、設(shè)置選中等等)
- javascript Select標(biāo)記中options操作方法集合
- js 觸發(fā)select onchange事件代碼
- javascript操作select參考代碼
- jquery及原生js獲取select下拉框選中的值示例
相關(guān)文章
原生JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播插件
這篇文章主要介紹了原生JS實(shí)現(xiàn)旋轉(zhuǎn)木馬式圖片輪播插件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04HTML+CSS+JavaScript實(shí)現(xiàn)可拖拽模態(tài)框
這篇文章主要為大家詳細(xì)介紹了HTML+CSS+JavaScript實(shí)現(xiàn)可拖拽模態(tài)框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07JavaScript獲取GridView選擇的行內(nèi)容
一般GridView第一列是多選框CheckBox,負(fù)責(zé)標(biāo)記當(dāng)前行是否被選中,后面可以有文本框TextBox,下拉框DropDownList,標(biāo)簽Lable2009-04-04WebStorm中如何將自己的代碼上傳到github示例詳解
這篇文章主要介紹了WebStorm中如何將自己的代碼上傳到github,本文分步驟通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10JS Generator函數(shù)yield表達(dá)式示例詳解
這篇文章主要為大家介紹了JS Generator函數(shù)yield表達(dá)式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10bootstrap中的 form表單屬性role="form"的作用詳解
這篇文章主要介紹了bootstrap中的 form表單屬性role="form"的作用詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01