AJAX二级下拉框联动

<SCRIPT type="text/javascript">
var req;
window.onload=function(){
}

function Change_Select(url, parentInputId, childInputId)
{
var parentInputValue = document.getElementById(parentInputId).value;
var showUrl = url + "&parentInputId="+ parentInputId + "&parentInputValue=" + parentInputValue;
//alert(showUrl);
showUrl=encodeURI(showUrl);
//alert(showUrl);
showUrl=encodeURI(showUrl);
//alert(showUrl);
if(window.XMLHttpRequest)
{
req=new XMLHttpRequest();
}else if(window.ActiveXObject)
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}

if(req)
{
req.open("GET",showUrl,true);
req.onreadystatechange= function() {
callback(childInputId);
}
req.send(null);
}
}

function callback(childInputId)
{
if(req.readyState == 4)
{
if(req.status == 200)
{
parseMessage(childInputId);
}else{
// Not able to retrieve descriptionInvalid URI: isHexDigit
alert("Not able to retrieve description"+req.statusText);
}
}
}

function parseMessage(childInputId)
{
//alert();
var xmlDoc=req.responseXML.documentElement;
if(xmlDoc == 'null') {
alert("xmlDoc="+xmlDoc);
}
var xSel=xmlDoc.getElementsByTagName('select');
var select_child=document.getElementById(childInputId);
var select_child_length = select_child.options.length;
for(var i = select_child_length-1; i >= 0; i--){
select_child.remove(i);
}

//alert("xSel.length="+xSel.length);
for(var i=0;i<xSel.length;i++)
{
var xValue=xSel[i].childNodes[0].firstChild.nodeValue;
var xText=xSel[i].childNodes[1].firstChild.nodeValue;
var option=new Option(xText,xValue);
try{
select_child.options.add(option);
}catch(e){
}
}


}
</SCRIPT>


HTML调用:
<select id="parentId" onChange="Change_Select('seriesTextboxAction.do', 'parentId', 'childId')">
<option value="">- Select One -</option>
<option value="111">111</option>
<option value="222">222</option>
</select>

<select id="childId">
<option value="">- Select One -</option>
</select>


JAVA(Action)调用:
List<DropTo> dropList = Data List(from DB or other file);
int childListSize = dropList.size();
response.setContentType("text/xml"); response.setHeader("Cache-Control","no-cache");

StringBuffer buffer = new StringBuffer();
buffer.append("<selects>");
if(childListSize > 0) {
for(int i = 0; i < childListSize; i ++) {
DropTo dropFromDB = dropList.get(i);
String showKey = dropFromDB.getShowKey();
String showValue = dropFromDB.getHiddenValue();
System.out.println("showKey="+showKey+" showValue="+showValue);
buffer.append("<select><value>").append(showKey).append("</value>");
buffer.append("<text>").append(showValue).append("</text></select>");
}
buffer.append("</selects>");
response.getWriter().write(buffer.toString());
response.getWriter().flush();
response.getWriter().close();
}


Struts:
<action name="seriesTextboxAction" class="xxxx.SeriesTextboxAction">
<result name="success"></result>
</action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值