怎么取得dropdownlist选中的值?大家救救我啊,我搞了一天了。
poily 2007-04-12 05:43:02 我找了用一个js 脚本(main.js),一个xml文件(serverDoc.xml)做的两个dropdownlist无刷新联动,要把其选中的值保存到数据库,可怎么也取不到选中的值。大家指点下怎么取得其选中的值。
// JScript 文件
/*main.js
var http_request=false;
var localPro;
var localCity;
var returnXML;
var returnText;
function initVar()
{
localPro=document.getElementById("province");
localCity=document.getElementById("city");
}
function createXMLHttp()
{
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
function sendRequest()
{
initVar();
createXMLHttp();
http_request.onreadystatechange = processRequest;
http_request.open("GET","serverDoc.xml", true);
http_request.send(null);
}
function processRequest()
{
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById("statusTxt").innerHTML="";
setProvince();
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}else {//只要未读取完成
document.getElementById("statusTxt").innerHTML="正则读取数据中……";
}
}
function setProvince()
{
returnText=http_request.responseText;
returnXML=http_request.responseXML;
var pro=returnXML.getElementsByTagName("title");
var citys=returnXML.getElementsByTagName("item");
var cityNodes=citys[0].getElementsByTagName("city");
enterValue(pro,localPro);
enterValue(cityNodes,localCity);
outputAll();
}
function loadCity()
{
var index=localPro.selectedIndex;
var cityNode=returnXML.getElementsByTagName("item");
var curCity=cityNode[index];
var cityNodes=curCity.getElementsByTagName("city");
enterValue(cityNodes,localCity);
outputAll();
}
function enterValue(cell,place)
{
clearPreValue(place);
for(i=0;i<cell.length;i++){
var obj= document.createElement("option");
var doc=document.createTextNode(cell[i].firstChild.nodeValue);
obj.appendChild(doc);
place.appendChild(obj);
}
}
function clearPreValue(pc)
{
while(pc.hasChildNodes())
pc.removeChild(pc.childNodes[0]);
}
function outputAll ()
{
var cellValue= localPro.options[localPro.selectedIndex].firstChild.nodeValue+localCity.options[localCity.selectedIndex].firstChild.nodeValue;
document.getElementById("district").value=cellValue;
}
xml文件:
<?xml version="1.0" encoding="GBK"?>
<china>
<province>
<title>北京</title>
<item>
<city>东城</city>
<city>西城</city>
</item>
</province>
<province>
<title>上海</title>
<item>
<city>黄浦</city>
<city>卢湾</city>
</item>
</province>
<province>
<title>其它</title>
<item>
<city>北美洲</city>
<city>大洋洲</city>
</item>
</province>
</china>
测试页面:
<script language="JavaScript" type="text/javascript" src="main.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body onload="sendRequest();">
<form id=form1 runat=server><div id="statusTxt"> </div>
<asp:DropDownList ID="province" runat="server" onchange="javascript:loadCity();" Width="98px"></asp:DropDownList>
<asp:dropdownlist ID="city" runat="server" onchange="javascript:outputAll();" Width="100px"></asp:dropdownlist>
</form>
</body>
</html>