area.js 1.68 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
function get(id,callback) {
$.get(APP.PATH+"/sysRegions/find?id="+id,function(data){
callback(data);
})
}

//下一个级别
function nextLeve(leve) {
var tmp = leve.substring(leve.length-1);
var intTmp = parseInt(tmp)+1;
return intTmp;
}


//移除当前列表的后续选项
function remove(leve) {
var max = 3;
var nextLeveInt = nextLeve(leve);
for(var i=nextLeveInt;i<=max;i++) {
$("#leve"+i).children().first().nextAll().remove();
}
}

function handler(data,leveName) {
var leve =data==null ? [] : data.records;
var nextLeveInt = nextLeve(leveName);
if(leve!=null && leve.length>0) {
remove(leveName);
for(var i=0;i<leve.length;i++) {
$("#leve"+nextLeveInt).append("<option id="+leve[i].id+" value="+leve[i].id+">"+leve[i].name+"</option>")
}
}
}

function handlerSelect(data,leveName,selectedId) {
//console.log(leveName,selectedId)
//if(selectedId===undefined)return;
var leve =data==null ? [] : data.records;
var nextLeveInt = nextLeve(leveName);
if(leve!=null && leve.length>0) {
remove(leveName);
for(var i=0;i<leve.length;i++) {
var tag = "";
if(leve[i].id==selectedId){
tag = "selected='selected'";
}
//console.log(tag,selectedId)
//console.log(leve[i].id,selectedId,tag)
$("#leve"+nextLeveInt).append("<option "+tag+" value="+leve[i].id+">"+leve[i].name+"</option>")
}
}
}

$(function(){

$(".leve").bind("change",function(){
var leveId = $(this).attr("id");
var leves = ["leve1","leve2","leve3"];
var thisVal = $(this).val();
var ZERO = "0";
if((leveId === leves[0] || leveId===leves[1]) &&thisVal===ZERO){
remove(leveId);
}else{
//获取数据
get(thisVal,function(data){
handler(data,leveId);
})
}
});
})