Ajax 应用模板(动态加载列表框)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>

<head>

           <META http-equiv=Content-Type content="text/html; charset=UTF-8" >

</head>

 

<script type="text/javascript">

      var XMLHttpReq;

      

//创建XMLHttpRequest对象

 

      if(window.XMLHttpRequest){ //Mozilla 浏览器
           xmlhttp = new XMLHttpRequest();
           if (xmlhttp.overrideMimeType){ //设置MiME类别
               xmlhttp.overrideMimeType('text/xml');
           }
      }else if (window.ActiveXObject){// IE浏览器
           try{ 

                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

                }catch (e){
                          try{

                               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

                          }catch (e){ }
                }
      }

 

 

//发送请求函数

 

     function sendRequest(url, data){  //初始化、指定处理函数、发送请求的函数
       if (!xmlhttp){ //异常,创建对象实例失败
        window.alert("不能创建XMLHttpRequest对象实例.");
        return false;
       }
 
       // 确定发送请求的方式和URL以及是否同步执行下段代码
       xmlhttp.open("POST", url, true);
       xmlhttp.onreadystatechange = processRequest1;  //根据Web服务器应答,触发该状态改变事件
       xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       xmlhttp.send("division=" + data);  //发送信息到后台程序
     }

 

//处理返回信息函数

 

function processRequest(){       

     if (xmlhttp.readyState == 4){ // 判断对象状态
          if (xmlhttp.status == 200)  //正常返回信息,状态编号200
          { // 信息已经成功返回,开始处理信息

                  updateList();

          }else{ //页面不正常
            alert("您所请求的页面有异常。");
          }

     }

}

 

//刷新列表框函数

function refreshList(){

     var sort = document.getElementById("sort").value;

     if(sort == ""){

          clearList();

          return; 

     }

     var url = "**?**="+**;

     sendRequest(url);

}

 

//更新列表框中列表函数

function updateList(){

     clearList();

     var product = document.getElementById("product");

     var results = XMLHttpReq.responseXML.getElementsByTagName("name");

     var option = null;

     for(var i=0;i<results.length;i++){

          option = document.createElement("option");

          option = appendChild(document.createTextNode(results[i]).firstChild.nodeValue));

          product.appendChild(option);

     }

}

 

//清楚列表框中原有选项的函数

function clearList(){

    var product = document.getElementById("product");

    while(product.childNodes.length > 0){

            product.removeChild(product.childNodes[0]);

    }

}

 

 

</script>

 

 

<table>

         <select id="sort" οnchange="refreshList();">

                   <option value="">请选者</option>

                   <option value="**">**</option>

                   <option value="**">**</option>

                   <option value="**">**</option>

         </select>

 

         <select id="product" >

          </select>

</table>

 

 

后台:

//设置输出信息的格式,字符

   response.setContentType("text/html; charset=UTF-8");

   response.setHeader("Cache-Control","no-cache");

//创建输出流对象

   PrintWriter out = response.getWriter();

 

   out.println("<response>");

   --

   --

   out.println("<name>"+**+"</name>");

   --

   --

   out.println("</response>"); 

   out.close();

 

//

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值