方法html
/** * 級聯 * 聯動 * @param url:訪問json數據的地址 * @param param:參數 * @param levelIds:頁面下拉標籤數組,爲聯動級數 * @private */ function _yh_linkage(url,params, levelIds){ _yh_postRequest(url,params,function(response){ //console.log(response); /** * 初始下拉列表數據 * @param obj * @returns {jQuery} */ function objInit(obj){ return $('#'+obj).html('<option value="">請選擇</option>'); } selectToChildOption(response,levelIds,0); /** * 遞歸聯動初始數據 * @param object:數據 * @param levelIds:聯動下拉框id數組 * @param levelIndex:第幾級下拉列表 */ function selectToChildOption(object,levelIds,levelIndex){ for (var index in levelIds) { if(index<levelIndex) continue; objInit(levelIds[index]); } if( object == null ) return ; if( levelIds == null || levelIds.length <= levelIndex ) return ; if( object != null && object.list != null ) { $.each(object.list,function(i,o){ $('#'+levelIds[levelIndex]).append('<option value="'+o.id+'" >'+o.name+'</option>'); }); $('#'+levelIds[levelIndex]).change(function() { var n = $('#'+levelIds[levelIndex]).get(0).selectedIndex-1; selectToChildOption(object.list[n], levelIds, levelIndex + 1); }); } } }); }
須要用到的另外一函數java
/** * post請求 * @param url * @param params * @param callbackfunciton:回調函數 * @returns */ function _yh_postRequest(url,params,callbackFunction){ //console.log(params); if(params == null ){ params = {}; } $.post(url,params,function(response){ if( callbackFunction != null) callbackFunction(response); }); }
javajson
public class TKY { private String id; private String name; private boolean select= false;//是否默認選中 private List<TKY> list = new ArrayList<>(); public TKY(String id ,String name){ this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public List<TKY> getList() { return list; } public void addList(TKY t) { list.add(t); } public boolean isSelect() { return select; } public void setSelect(boolean select) { this.select = select; } }