ajax和struts实现级联

<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlhttp;
function createXMLHttp(){
if(window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
}

function getLevel2(){
createXMLHttp();
url="info.do?id="+document.forms.form1.select1.value;
alert(url);
xmlhttp.open("get",url,true);
xmlhttp.onreadystatechange=function callback(){
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{

addOption();
}
}
};
xmlhttp.send(null);
}
function clearlist(){
var models=document.getElementById("select2");
while(models.childNodes.length>0){
models.removeChild(models.childNodes[0]);
}
}
function addOption(){
clearlist();
createXMLHttp();
var xmlDoc=xmlhttp.responseXML;
alert(xmlDoc);
var results=xmlDoc.getElementsByTagName("option");
for(var i=0;i<results.length;i++){
var xValue=results[i].childNodes[0].firstChild.nodeValue;
var xText=results[i].childNodes[1].firstChild.nodeValue;
var option=new Option(xText,xValue);
models.appendChild(option);
}
}

</script>
</head>

<body>
<form action="#" name="form1">
<select name="select1" οnchange="getLevel2();">
<option value="0">
请选择一级目录
</option>
<%
List<Info> list = InfoDao.getFirst();
for (Info info : list) {
request.setAttribute("info", info);
%>
<option value="${info.id }">
${info.name }
</option>
<%
}
%>
</select>
<select name="select2">
<option value="0">
请选择二级目录
</option>
</select>
</form>
<br>
</body>
</html>


action代码
public class InfoAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String id=request.getParameter("id");
Info info1=InfoDao.getInfo(id);
List<Info> list=(ArrayList<Info>)InfoDao.getNext(info1);
String xml_start="<?xml version=\"1.0\" encoding=\"UTF-8\"?><selects>";
String xml_end="</selects>";
String xml="";
for(Info info : list){
xml+="<option value='"+info.getId()+"'>'"+info.getName()+"'</option>";
}
String last_xml=xml_start+ xml+ xml_end;
try {
response.getWriter().write(last_xml);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(last_xml);
return mapping.findForward("ok");
}
}为什么js中addOption()在callback()中调用不起作用啊?求高手指导
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值