//// 默认下拉框 // option: 'city' 城市三级联动 function City(params){ this.params = params; this.sel(); switch(params.option){ case 'city': this.city(); break; default: break; } }; // 下拉 City.prototype.sel = function(){ var that = this; this.select_ele = $('.select'); this.option_ele = $('.option'); this.optionLi_ele = $('.select .option ul li'); // 更新变量 this.updata = function () { this.select_ele = $('.select'); this.option_ele = $('.option'); this.optionLi_ele = $('.select .option ul li'); } // 下拉框 this.select = function () { this.select_ele.click(function (e) { e.stopPropagation(); //that.option_ele.stop().slideUp(200); //$(this).find(".option").stop().slideDown(200); }) this.optionLi_ele.click(function (e) { e.stopPropagation(); var _this = $(this); var inp_val = $(this).text(); _this.parents('.select').find('input').val(inp_val); _this.parents('.option').stop().slideUp(200); }) $(document).click(function () { that.option_ele.stop().slideUp(200); }) }; this.select(); }; City.prototype.city = function(){ // city var _this = this; this.city_data = {}, this.sheng_arr = [], this.shi_arr = [], this.getJSON = function(){ $.getJSON("/javascript/city.json",function(data){ _this.city_data = data; _this.deal_data(data); }) } this.getJSON(); }; // 处理返回数据 City.prototype.deal_data = function(data){ for(var code in data){ var sheng = code.substring(2); var shi = code.substring(4); if(sheng == '0000'){ var sheng_obj = { 'code': code, 'value': data[code] } this.sheng_arr.push(sheng_obj); }else if(shi == '00'){ var shi_obj = { 'code': code, 'value': data[code] } this.shi_arr.push(shi_obj); } } this.deal_sheng(); }; // 省 City.prototype.deal_sheng = function(){ var sheng_box = $(this.params.ele).find('.sheng ul'); var _this = this; sheng_box.find('li').remove(); for(var i=0; i'+ sheng_name +''; sheng_box.append(sheng_str); } this.updata(); this.select(); sheng_box.find('li').click(function(){ var check_city = { code: $(this).attr('data-code'), name: $(this).text() } _this.deal_shi(check_city); }) }; // 市 City.prototype.deal_shi = function(check_city){ var shi_box = $(this.params.ele).find('.shi'); var _this = this; shi_box.find('ul li').remove(); var reg = /市$/; if(reg.test(check_city.name)){ var shi_value = check_city.name.substring(0,check_city.name.length - 1); var shi_code = check_city.code.substring(0,2)+'0100'; var shi_str = '
  • '+ shi_value +'
  • '; shi_box.find('ul').append(shi_str); shi_box.find('input').val(shi_value); this.deal_qu(shi_code); }else{ var tmp_arr = []; var find_code = check_city.code.substring(0,2); for(var j=0; j'+ this.shi_arr[j].value +''; shi_box.find('ul').append(shi_str); } } shi_box.find('input').val(tmp_arr[0].value); this.deal_qu(tmp_arr[0].code); } this.updata(); this.select(); shi_box.find('ul li').click(function(){ _this.deal_qu($(this).attr('data-code')); }) }; // 区 City.prototype.deal_qu = function(code){ var qu_box = $(this.params.ele).find('.qu'); qu_box.find('ul li').remove(); var find_code1 = code.substring(0,4); var tmp_arr = []; for(var shi in this.city_data){ var shi_1 = shi.substring(0,4); var shi_2 = shi.substring(shi.length - 2,shi.length); if(shi_1 == find_code1 && shi_2 != '00'){ var shi_data = { code: shi, name: this.city_data[shi] } tmp_arr.push(shi_data); } } qu_box.find('input').val(tmp_arr[0].name); for(var i=0; i'+ tmp_arr[i].name +''; qu_box.find('ul').append(qu_str); } this.updata(); this.select(); }; new City({ option: 'city', ele: '.tai_city' }); new City({ option: 'city', ele: '.open_city' }); new City({ option: 'city', ele: '.address_city' }); new City({ option: 'city', ele: '.enroll_city' });