Posted on 2008-09-01 00:18
橡皮人 阅读(457)
评论(0) 编辑 收藏
一个简单的纯手工的Ajax&&JSP二级联动。
<script type="text/javascript">
var req;
window.onload=function()
{//页面加载时的函数
}
function Change_Select(){//当第一个下拉框的选项发生改变时调用该函数
var province = document.getElementById('fatherType').value;
var url = "/nicholas/select?fatherType="+province;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}else if(window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
//指定回调函数为callback
req.onreadystatechange = callback;
req.send(null);
}
}
//回调函数
function callback(){
if(req.readyState ==4){
if(req.status ==200){
parseMessage();//解析XML文档
}else{
alert("不能得到描述信息:" + req.statusText);
}
}
}
//解析返回xml的方法
function parseMessage(){
var xmlDoc = req.responseXML.documentElement;//获得返回的XML文档
var xSel = xmlDoc.getElementsByTagName('select');
//获得XML文档中的所有<select>标记
var select_root = document.getElementById('childType');
//获得网页中的第二个下拉框
select_root.options.length=0;
//每次获得新的数据的时候先把每二个下拉框架的长度清0
for(var i=0;i<xSel.length;i++){
var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
//获得每个<select>标记中的第一个标记的值,也就是<value>标记的值
var xText = xSel[i].childNodes[1].firstChild.nodeValue;
//获得每个<select>标记中的第二个标记的值,也就是<text>标记的值
var option = new Option(xText, xValue);
//根据每组value和text标记的值创建一个option对象
var option2=String.fromCharCode(option);
try{
select_root.add(option);//将option对象添加到第二个下拉框中
}catch(e){
}
}
}
</script>
父类别:
<select name="fatherType" id="fatherType" onChange="Change_Select()">
<% for (int i = 0; i < list.size(); i++) {
father = (FatherType) list.get(i);
String selected = "";
%>
<option value="<%=father.getType()%>"><%=father.getType()%></option>
<%
}
%>
</select> <br>
子类别:
<select id="childType" name="childType">
<option></option>
</select>
下面是nicholas项目下的select Servlet
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml"); //xml传递过来的参数是以UTF-8编码
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("gbk");
String targetId = request.getParameter("fatherType");
targetId=new String(targetId.getBytes("ISO-8859-1"),"gbk");//将已经乱码的参数转码为中文
// 获得请求中参数为id的值
String xml_start = "<selects>";
String xml_end = "</selects>";
String xml = "";
List list = null;
ChildType child = null;
TypeDAO typedao = new TypeDAO();
list = typedao.getChildType(targetId); //根据父类型参数查询出子类型参数
for (int i = 0; i < list.size(); i++) {
child = (ChildType) list.get(i);
xml += "<select><value>" + child.getType() + "</value><text>"
+ child.getType() + "</text></select>";
}
String last_xml = xml_start + xml + xml_end;
response.setContentType("text/xml;charset=utf-8"); //将中文参数传递时先转码为UTF-8
PrintWriter out = response.getWriter();
out.write(last_xml);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}