客户端触发:

<select id="category1" onchange="changecategory()">
    <option value="-1">所有一级类别</option>
   <%
    for(int i=0; i<topCategorys.size(); i++) {
     Category c = topCategorys.get(i);
   %>
      <option value="<%=c.getId() %>"><%=c.getName() %></option>
   <%
    }
    %>
   </select>
   <select id="category2">
    <option value="-1">所有二级类别</option>
   </select>

创建XHR:
<script type="text/javascript">
  var req;
  function changecategory() {
   if(window.XMLHttpRequest) {
    req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   var id = document.getElementById("category1").options[document.getElementById("category1").selectedIndex];
   var url = "getchildcategory.jsp?id=" + escape(id.value);

req.open("GET", url, true);
   req.onreadystatechange=callback;
   req.send(null);
  }
  function callback() {
   if (req.readyState == 4) {
    if (req.status == 200) {

  //parseXml(req.responseXML);//方式1:XML方式(太麻烦)

  //parse(req.responseText);//方式2:字符方式(最常用)
     eval(req.responseText);//方式3:动态生成javascript方式(最常用)
      } 
     }
  }

function parseXml(msg) {
   var categorys = msg.getElementsByTagName("categorys")[0];
   //alert(categorys.childNodes.length);
   document.getElementById("category2").length=categorys.childNodes.length+1;
   document.getElementById("category2").selectedIndex=0;
   for(var i=0; i<categorys.childNodes.length; i++) {
    var category = categorys.childNodes[i];
    var id = category.childNodes[0].childNodes[0].nodeValue;
    //alert(id);
    var name = category.childNodes[1].childNodes[0].nodeValue;
    //alert(name);
    document.getElementById("category2").options[i+1].value=id;
    document.getElementById("category2").options[i+1].text=name;
   }
  }

function parse(msg) {
   //alert("|" + msg + "|");
   msg = msg.replace(/(^\s*)|(\s*$)/g, "");
   if(msg == null || new String(msg) == "") {
    return;
   }
   var categorys = msg.split("-");
   for(var i=0; i<categorys.length; i++) {
    var category = categorys[i].split("&");
    var id = category[0];
    var name = category[1];
    document.getElementById("category2").length=categorys.length+1;
    document.getElementById("category2").selectedIndex=0;
    document.getElementById("category2").options[i+1].value=id;
    document.getElementById("category2").options[i+1].text=name;
   }
  }
 </script>

server处理方式(getchildcategory.jsp中的内容),返回包含处理结果的javascript代码:

<%
 String strId = request.getParameter("id");
 int id = -1;
 if(strId != null && !strId.trim().equals("")) {
  id = Integer.parseInt(request.getParameter("id"));
 }
 Category parent = CategoryMgr.getInstance().loadById(id);
 List<Category> childs = parent.getChilds();
 StringBuffer buff = new StringBuffer();

//方式1:XML方式

/*

for(int i=0; i<childs.size(); i++) {
  Category c = childs.get(i);
  buff.append("<category><id>" + c.getId() + "</id><name>" + c.getName() + "</name></category>");
 }
 buff.insert(0,"<categorys>");
 buff.append("</categorys>");

*/

//方式2:字符方式

/*

for(int i=0; i<childs.size(); i++) {
  Category c = childs.get(i);
  buff.append(c.getId() + "&" + c.getName() + "-");
 }
 if(childs.size() > 0){
  buff.deleteCharAt(buff.length()-1);
 }
 */

//方式3:动态生成javascript方式
 for(int i=0; i<childs.size(); i++) {
  Category c = childs.get(i);
  buff.append("document.getElementById('category2').options[" + (i+1) + "].value=" + c.getId() + ";\n");
  buff.append("document.getElementById('category2').options[" + (i+1) + "].text='" + c.getName() + "';\n");
 }
 buff.insert(0, "document.getElementById('category2').selectedIndex=0;\n");
 buff.insert(0, "document.getElementById('category2').length=" + (childs.size()+1) +";\n");
 
System.out.println(buff.toString());

//可以采用清除缓存的方法,如下
 response.setContentType("text/xml");
 response.setHeader("Cache-Control", "no-store"); //HTTP1.1 
 response.setHeader("Pragma", "no-cache"); //HTTP1.0
 response.setDateHeader("Expires", 0);
 response.getWriter().write(buff.toString());
%>

下拉条的连动-ajax方式的更多相关文章

  1. 通过Ajax方式上传文件,使用FormData进行Ajax请求

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

  2. ajax方式提交带文件上传的表单,上传后不跳转

    ajax方式提交带文件上传的表单 一般的表单都是通过ajax方式提交,所以碰到带文件上传的表单就比较麻烦.基本原理就是在页面增加一个隐藏iframe,然后通过ajax提交除文件之外的表单数据,在表单数 ...

  3. OSS web直传 ajax方式 上传图片、文件

    部分js代码 send_request = function(){//这是从后台获取认证策略等信息. var htmlobj=$.ajax({url:root+"/service/polic ...

  4. [转] 通过Ajax方式上传文件,使用FormData进行Ajax请求

    通过传统的form表单提交的方式上传文件: <form id= "uploadForm" action= "http://localhost:8080/cfJAX_ ...

  5. 百度编辑器ueditor通过ajax方式提交,不需要事先转义字符的方法(异常:从客户端(xxx)中检测到有潜在危险的 Request.Form 值)

    最近项目中使用百度编辑神器ueditor,确实是很好用的一款编辑器.官网教程提供的与后端数据交互都是跟表单方式有关的,项目中使用的是ajax方式提交,因此出现了不少问题,现在记录备忘下. 环境:.ne ...

  6. jquery.validate ajax方式验证

    在做网站的时候有一块需要用到jquery.validate插件 ajax方式的方式来验证原始密码是否正确,研究了研究加上博客园朋友的帮助,终于实现了.贴出代码 <script type=&quo ...

  7. [实战]MVC5+EF6+MySql企业网盘实战(5)——ajax方式注册

    写在前面 今天贴合到实际的客户需求仔细的想了下,其实在userInfo这个类里面很多字段都不是必须的.有很多的事业单位根本就不能上网,填写的邮箱也是exchange的,个人的详细信息都在ad里面可以取 ...

  8. python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)

    一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...

  9. 阿里OSS ajax方式 web直传

    部分js代码 send_request = function(){//这是从后台获取认证策略等信息. var htmlobj=$.ajax({url:root+"/service/polic ...

随机推荐

  1. c# 面相对象4-多态性

    一.定义: 多态是面向对象程序设计的又一个特性.在面向过程的程序设计中,主要工作是编写一个个的过程或函数,这些过程和函数不能重名.例如在一个应用中,需要对数值型数据进行排序,还需要对字符型数据进行排序 ...

  2. [Effective Modern C++] Item 3. Understand decltype - 了解decltype

    条款三 了解decltype 基础知识 提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪. 以下是一些简单的推断类型: ; // decltype(i) -> ...

  3. JS数组删除一个元素(根据值删)

    <script type="text/javascript"> <!-- // 删除数组中第一个匹配的元素,成功则返回位置索引,失败则返回 -1. Array.p ...

  4. 【转】aiohttp 源码解析之 request 的处理过程

    [转自 太阳尚远的博客:http://blog.yeqianfeng.me/2016/04/01/python-yield-expression/] 使用过 python 的 aiohttp 第三方库 ...

  5. 今日哈工大刷推荐python脚本

    import httplib import random import time import urllib2 import re address = raw_input("Please i ...

  6. Mfgtool

    For bootstrap mode, it refers to the communcation between the host and ROM codes through serial down ...

  7. ASCII、ANSI、GB2312、Unicode、UTF-8之间的关系

    1.ASCII码: ASCII(American Standard Code for Information Interchange,美国信息互换标准代码)是基于拉丁字母的一套电脑编码系统.它主要用于 ...

  8. js跑马灯效果

    function nextPage() {           /*         克隆第一张图片并添加到box后         box前移一张图片的距离动画         动画回调里把box的 ...

  9. delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)(通过修改OpenTool生效)

    delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...

  10. win7系统怎样备份

    利用系统自带的备份还原 1 这种方法的缺点是如果以后系统出现问题,无法进入系统的话,就无法恢复系统了.首先我们点击开始菜单,打开控制面板! 2 在控制面板中点击系统和安全! 3 我们选择备份和还原中的 ...