sunlips 发表于 2013-1-29 10:32:01

ajax动态生成select

auto.jsp
<%@ page contentType="text/html; charset=gb2312" %><%@ page import="com.kaka.vo.OnLineUser,java.util.*"%><jsp:useBean id="onLineUser" class="com.kaka.vo.OnLineUser" scope="application" /><%//设置输出信息的格式及字符集 //自动更新在线人数、当前访问量、空闲用户idresponse.setContentType("text/xml; charset=UTF-8");response.setHeader("Cache-Control","no-cache");out.println("<respose>");out.println("<option>");out.println("<value>"+onLineUser.getFreeUserId(0)+"</value>");out.println("<text>"+onLineUser.getFreeUserId(0)+"</text>");out.println("</option>");out.println("<option>");out.println("<value>"+onLineUser.getFreeUserId(1)+"</value>");out.println("<text>"+onLineUser.getFreeUserId(1)+"</text>");out.println("</option>");out.println("<option>");out.println("<value>"+onLineUser.getFreeUserId(2)+"</value>");out.println("<text>"+onLineUser.getFreeUserId(2)+"</text>");out.println("</option>");out.println("</respose>");%>


autoRefresh.jsp
<%@ page contentType="text/html; charset=gb2312" %><script language="javascript">var XMLHttpReq; //创建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 sendRequest() {createXMLHttpRequest();      var url = "auto.jsp";XMLHttpReq.open("GET", url, true);XMLHttpReq.onreadystatechange = processResponse;//指定响应函数XMLHttpReq.send(null);// 发送请求}// 处理返回信息函数    function processResponse() {    if (XMLHttpReq.readyState == 4) { // 判断对象状态      if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息DisplayHot();setTimeout("sendRequest()", 1000);            } else { //页面不正常                window.alert("您所请求的页面有异常。");            }      }    }    function DisplayHot() {    //var one = XMLHttpReq.responseXML.getElementsByTagName("one").firstChild.nodeValue;    //var two = XMLHttpReq.responseXML.getElementsByTagName("two").firstChild.nodeValue;var str = "";var options = XMLHttpReq.responseXML.getElementsByTagName("option");for (var i=0; i<options.length; i++){var optionData = XMLHttpReq.responseXML.getElementsByTagName("option");var value = optionData.getElementsByTagName("value").firstChild.nodeValue;var text =optionData.getElementsByTagName("text").firstChild.nodeValue;str+="<option value='"+value+"'>"+text+"</option>";}document.getElementById("onLineUsers").innerHTML = str;}</script><body onload =sendRequest()><table style="BORDER-COLLAPSE: collapse" borderColor=#111111 cellSpacing=0 cellPadding=0 width=200    bgColor=#f5efe7 border=0><TR>   <TD align=middle bgColor=#dbc2b0 height=19 colspan="2"><B>在线人数</B> </TD></TR><tr>   <td height="20"> 1:</td>   <td height="20" id="one"> </td></tr><tr>   <td height="20"> 2:</td>   <td height="20" id="two"> </td></tr><tr>   <td height="20"> 3:</td>   <td height="20" id="three"> </td></tr><%   String strPath = (String)request.getContextPath();   out.println("当前路径:"+strPath);%><select size="5" id="onLineUsers"></select></body> </table>
页: [1]
查看完整版本: ajax动态生成select