在做項(xiàng)目的時(shí)候,我們經(jīng)常需要用到地址之類的省市區(qū)三級(jí)聯(lián)動(dòng)的,今天就給大家分享一個(gè)非常簡(jiǎn)潔的省市區(qū)三級(jí)聯(lián)動(dòng)的代碼,基于jQuery,附上GIT地址,有需要的小伙伴可以直接拿走
省市級(jí)聯(lián)動(dòng),附使用示例和數(shù)據(jù)表數(shù)據(jù)
有部分?jǐn)?shù)據(jù)精確到鄉(xiāng)鎮(zhèn)一級(jí)!!!
Git 地址:http://git.oschina.net/upliu/province-city-district
演示代碼:
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title>省市區(qū)三級(jí)聯(lián)動(dòng)</title>
- </head>
- <body>
- <form method="post" action="post.php">
- <div id="area"></div>
- <input type="submit" style="margin-top: 10px;">
- </form>
- <script src="jquery-2.1.3.min.js"></script>
- <script src="area.js"></script>
- <script>
- $(function(){
- add_select(0);
- $('body').on('change', '#area select', function() {
- var $me = $(this);
- var $next = $me.next();
- /**
- * 如果下一級(jí)已經(jīng)是當(dāng)前所選地區(qū)的子地區(qū),則不進(jìn)行處理
- */
- if ($me.val() == $next.data('pid')) {
- return;
- }
- $me.nextAll().remove();
- add_select($me.val());
- });
- function add_select(pid) {
- var area_names = area['name'+pid];
- if (!area_names) {
- return false;
- }
- var area_codes = area['code'+pid];
- var $select = $('<select>');
- $select.attr('name', 'area[]');
- $select.data('pid', pid);
- if (area_codes[0] != -1) {
- area_names.unshift('請(qǐng)選擇');
- area_codes.unshift(-1);
- }
- for (var idx in area_codes) {
- var $option = $('<option>');
- $option.attr('value', area_codes[idx]);
- $option.text(area_names[idx]);
- $select.append($option);
- }
- $('#area').append($select);
- };
- });
- </script>
- </body>
- </html>
以上所述就是本文給大家分享的全部?jī)?nèi)容了,希望大家能夠喜歡。
新聞熱點(diǎn)
疑難解答
圖片精選