XmlHttpRequest用法
function getDepartmentForSelect() { createXMLHttpRequest(); var url = '../xxx.do'; XMLHttpReq.open("GET", url, true); XMLHttpReq.onreadystatechange = handleDepartment;//指定响应函数 XMLHttpReq.send(null); } function handleDepartment() { if (XMLHttpReq.readyState == 4) { // 判断对象状态 if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息 onreadyDepartment(); } else { //页面不正常 window.alert("您所请求的页面有异常。"); } } } //创建XMLHttpRequest对象 function createXMLHttpRequest() { if (window.XMLHttpRequest) { //Mozilla 浏览器 XMLHttpReq = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE浏览器 try { XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } } function onreadyDepartment() { var xmlResponse = XMLHttpReq.responseXML; var departments = xmlResponse.getElementsByTagName("department"); if (departments.length == 0) { return; } var queryDepartment = document.getElementById("departmentHidden").value; var objSelect = document.getElementById("department"); var departmentArray = departments[0].firstChild.text.split(","); var varItemHead = new Option(); objSelect.options.add(varItemHead); //头部 for (var i = 0; i < departmentArray.length; i++) { var varItem = new Option(departmentArray[i], departmentArray[i]); if (queryDepartment == departmentArray[i]) { varItem.selected = true; } objSelect.options.add(varItem); } } getDepartmentForSelect();
<input type="hidden" value="<%=queryDepartment%>" id="departmentHidden"/> <select name="department" id="department" style="width:90px;"></select>
String returnDepartment = ""; List<String> departmentList = DepartmentPartDAO.getDepartmentList(); StringBuilder departmentSB = new StringBuilder(); for (String str:departmentList){ departmentSB.append(str+","); } if(departmentSB.length()>0){ returnDepartment = departmentSB.substring(0,departmentSB.length()-1); } PrintWriter out; try { response.setContentType("text/xml;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); out = response.getWriter(); out.println("<response>"); out.println("<department>" + returnDepartment + "</department>"); out.println("</response>"); out.close(); } catch (IOException e) { e.printStackTrace(); } return null;
request.setAttribute("queryDepartment", InterceptPartQueryForm.getDepartment());
相关推荐
jiaguoquan00 2020-07-07
zhaolisha 2020-06-12
坚持着执着 2020-06-05
knightwatch 2020-04-25
zhaolisha 2020-01-11
zhaolisha 2019-12-29
wanghongsha 2019-12-06
小小大人物 2014-01-19
loopstang 2014-01-11
roodyszz0 2019-11-17
81493369 2019-11-04
Liuzhiqianblog 2019-11-03
用不完的好奇心 2019-10-26
89241846 2019-09-12
青菜萝卜白菜 2019-07-03
荒唌Blog 2017-12-18
无材可去补苍天 2019-06-12
HAcSeeking 2018-11-05