我找了用一个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>
// 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>
解决方案 »
- 上传至服务器后偶尔出现的问题(VS2005 .net C#)
- 多页面传值问题,棘手呀
- 可以在Global.asax中替换每个输出页面中一段内容吗?
- (菜鸟)DataGrid控件的数据绑定怎么做的?
- XSLT在net2003的使用,散分20
- DATALIST分页的时候怎么样绑定不同的数据源?
- 如何通过post发送数据后得到返回的结果?()
- Asp.Net 控件套装 有演示,有下载
- 查询userid,password是否在数据库中 求大神指教
- 如何在asp.net中随机生成一串不重复的数字
- 如何在一个服务器上做2个带有数字证书的网站?
- 请问:在vs2003中,用web service写了个方法,返回一个DataSet.asp中如何处理DataSet中的数据?
你的dropdownlist是服务器控件,转换成html(<select></select>)以后,再用JS操作它的option从界面上看是没有问题的,但是后台程序是不认识他的option的,他不是后台对象,所以这样是没办法的解决办法1:在后台不用dropdownlist,直接拼写<select><option></option></select>,最后后台通过Request.form[""]获取
解决办法2:选中一个值后仍然可以使用Request.form[""]获取,但要注意它的Name,因为Request.form是通过Name获取的
解决办法3:选中一个值以后,用JS操作的,同时把这个值保存在一个隐藏域里面,再去获取
<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>
<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>
localCity=document.getElementById("city").value;
<%--<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
selectedindex你看看有好几个这样的属性
这是为什么,请指点。尤其感谢“sos110(忙糊涂了)”“net_lover(【孟子E章】) ”
{
localPro=document.getElementById("province");
localCity=document.getElementById("city");
}
.net控件在html里看到的不是这个ID了。应该是document.getElementById("<%=province.ClientID%>")这样才能获得这个控件的ID