<select class="select" id="select"> </select>
var arr = [ { clevel:"1", id:"10", name:"公司", pId:"0" }, { clevel:"2", id:"10-100", name:"测试部", pId:"10" }, { clevel:"3", id:"10-100-100", name:"测试一部", pId:"10-100" }, { clevel:"4", id:"10-100-100-100", name:"测试一部分部一", pId:"10-100-100" }, { clevel:"3", id:"10-100-101", name:"测试二部", pId:"10-100" }, { clevel:"4", id:"10-100-101-100", name:"测试二部分部一", pId:"10-100-101" }, { clevel:"2", id:"10-101", name:"人事部", pId:"10" }, { clevel:"2", id:"10-102", name:"综合部", pId:"10" }, { clevel:"2", id:"10-103", name:"国库部", pId:"10" }, { clevel:"2", id:"10-104", name:"支付部", pId:"10" }, { clevel:"2", id:"10-106", name:"机关部", pId:"10" } ] var select = document.getElementById('select'); var opHtml = ''; opHtml += "<option value ='0'>请选择</option>"; var map = {}; for (var i = 0; i < arr.length; i++) { var item = { pid: arr[i].pId, id: arr[i].id, name: arr[i].name }; map[arr[i].id] = item; var index = item; var name = index.name; while (map[index.pid] != null) { index = map[index.pid]; name = index.name + "-" + name; } opHtml += "<option name='" + arr[i].name + "' value=1>" + name + "</option>"; } select.innerHTML = opHtml;