function initSelect(name, url, defaultValue) { $.ajax({ url : url, type : "post", dataType : "json", success : function(data) { var arrs = [ { id : "0", text : "请选择所属楼宇名称" } ]; $.each(data, function(i, item) { var arr = { id : item.id, text : item.name } arrs.push(arr); }); $('#' + name).select2({ width : '100%', allowClear : true, multiple : false, minimumResultsForSearch : -1, data : arrs }); $("#" + name).select2("val", defaultValue); } });
initSelect("strbdname", "buildingAjax!loadBuilding2", "0");