后台代码是怎样的
public String findByid() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest()
HttpServletResponse response=ServletActionContext.getResponse()
response.setContentType("text/xmlcharset=utf-8")
response.setHeader("Pragma", "no-cache")
response.setDateHeader("Expires", 0)
String xml_start = "<citys>"
String xml_end = "</citys>"
String xml = ""
Branchinfo infod=BranchServices.BranchByid(breanchid)
Set set=infod.getDepartinfos()
Iterator<Departinfo>iterm=set.iterator()
if(infod.getBranchid()==breanchid){
while (iterm.hasNext()) {
Departinfo depart =iterm.next()
xml += "<city><value>"
+ depart.getDepartId()+ "</value><text>"
+ depart.getDepartname()+"</text></city>"
}
}
String last_xml = xml_start + xml + xml_end
response.getWriter().write(last_xml)
response.getWriter().flush()
response.getWriter().close()
return "findDatescuess"
}
前台是这样(page1.jsp)
function getResult() {
var url = "Branchinfo_findByid.action?breanchid="+document.forms.LevelMenu.select1.value
if (window.XMLHttpRequest) {
XMLHttpReq = new XMLHttpRequest()
}else if (window.ActiveXObject) {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP")
}
if(XMLHttpReq){
XMLHttpReq.open("GET",url, true)
XMLHttpReq.onreadystatechange = complete
XMLHttpReq.send(null)
}
}
function complete(){
if(XMLHttpReq.readyState==4){
if(XMLHttpReq.status==200){
var xmlDoc=XMLHttpReq.responseXML//获取返回的XML文档
var citys=xmlDoc.getElementsByTagName("city")//获取XML节点
for(var i=0i<citys.lengthi++){//用循环给复选框中加值
var xValue = citys[i].childNodes[0].firstChild.nodeValue
var xText = citys[i].childNodes[1].firstChild.nodeValue
//写到这里就不知道如何用循环生成复选框 }else{
alert( "请求异常"+XMLHttpReq.responseText)
}
}
}
CheckBox chk = new CheckBox()chk.Text = "test" + i.ToString() // 这里可以换成数据库的内容
document.forms["form1"].Controls.Add(chk)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)