怎么取得dropdownlist选中的值?大家救救我啊,我搞了一天了。
我找了用一个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>
[解决办法]
你的代码我没看,不过从你的文字内容我已经明白什么意思了
你的dropdownlist是服务器控件,转换成html( <select> </select> )以后,再用JS操作它的option从界面上看是没有问题的,但是后台程序是不认识他的option的,他不是后台对象,所以这样是没办法的
解决办法1:在后台不用dropdownlist,直接拼写 <select> <option> </option> </select> ,最后后台通过Request.form[ " "]获取
解决办法2:选中一个值后仍然可以使用Request.form[ " "]获取,但要注意它的Name,因为Request.form是通过Name获取的
解决办法3:选中一个值以后,用JS操作的,同时把这个值保存在一个隐藏域里面,再去获取
[解决办法]
localPro=document.getElementById( "province ").value;
localCity=document.getElementById( "city ").value;
[解决办法]
form id=form1 runat=server>
<%-- <select name= "province " id= "province " onchange= "javascript:loadCity(); " runat=server>
<option value= "0 " selected= "selected "> 请选择 </option>
</select>
<select name= "city " id= "city " onchange= "javascript:outputAll(); " runat=server>
<option value= "0 " selected= "selected "> 请选择 </option>
</select> --%>
<div id= "statusTxt "> </div> </form>
这样直接用 selectde.value
[解决办法]
Request.Form[ "province "]
[解决办法]
.selected.value
[解决办法]
帮顶了
[解决办法]
帮你顶一下
[解决办法]
function initVar()
{
localPro=document.getElementById( "province ");
localCity=document.getElementById( "city ");
}
.net控件在html里看到的不是这个ID了。应该是document.getElementById( " <%=province.ClientID%> ")这样才能获得这个控件的ID