六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 28|回复: 0

ajax动态生成select

[复制链接]

升级  13.33%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
70
 楼主| 发表于 2013-1-23 01:17:27 | 显示全部楼层 |阅读模式
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")[0].firstChild.nodeValue;    //var two = XMLHttpReq.responseXML.getElementsByTagName("two")[0].firstChild.nodeValue;var str = "";var options = XMLHttpReq.responseXML.getElementsByTagName("option");for (var i=0; i<options.length; i++){var optionData = XMLHttpReq.responseXML.getElementsByTagName("option")[i];var value = optionData.getElementsByTagName("value")[0].firstChild.nodeValue;var text =  optionData.getElementsByTagName("text")[0].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>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表