jsp代码
<table width=600 align="center" border=0> <tr>
<td align="center" height=40>一级分类:</td>
<td><select name="parentId" id="parentId" οnchange="Change_Select2()">
<option value=0>==请选择==</option>
<c:forEach items="${requestScope.list}" var="c">
<option value="${c.id }">${c.cname }</option>
</c:forEach>
</select></td>
<td align="center">二级分类:</td>
<td><select name="parentId1" id="parentId1" οnchange="Change_Select3()">
<option value=0>==请选择==</option>
</select></td>
</tr>
<tr> <td align="right" colspan=2 height=40>参数:</td>
<td align="left" colspan=2><select name="bore" id="bore">
<option selected="selected" value=0>==请选择==</option>
</select></td>
</tr>
<table>
js代码
function Change_Select2(){//当第一个下拉框的选项发生改变时调用该函数
var province = document.getElementById('parentId').value;
var url = "searchCategory.do?id="+ escape(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 = callback2;
req.send(null);
}
}
//回调函数
function callback2(){
if(req.readyState ==4){
if(req.status ==200){
parseMessage2();//解析XML文档
}else{
alert("不能得到描述信息:" + req.statusText);
}
}
}
//解析返回xml的方法
function parseMessage2(){
var xmlDoc = req.responseXML.documentElement;//获得返回的XML文档
var xSel = xmlDoc.getElementsByTagName('select');
//获得XML文档中的所有<select>标记
var select_root = document.getElementById('parentId1');
//获得网页中的第二个下拉框
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对象
try{
select_root.add(option);//将option对象添加到第二个下拉框中
}catch(e){
}
}
}
function Change_Select3(){//当第一个下拉框的选项发生改变时调用该函数
var province = document.getElementById('parentId1').value;
var url = "searchbore.do?id="+ escape(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 = callback3;
req.send(null);
}
}
//回调函数
function callback3(){
if(req.readyState ==4){
if(req.status ==200){
parseMessage3();//解析XML文档
}else{
alert("不能得到描述信息:" + req.statusText);
}
}
}
//解析返回xml的方法
function parseMessage3(){
var xmlDoc = req.responseXML.documentElement;//获得返回的XML文档
var xSel = xmlDoc.getElementsByTagName('select');
//获得XML文档中的所有<select>标记
var select_root = document.getElementById('bore');
//获得网页中的第二个下拉框
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对象
try{
select_root.add(option);//将option对象添加到第二个下拉框中
}catch(e){
}
}
}