您的位置:首页 > 其它

Ajax实现省市县三级联动

2010-01-06 16:39 633 查看
Default.aspx.cs页面全部代码

Default.aspx.cs页面全部代码
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ajax实现二级联动</title>
<script type ="text/javascript">
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
if (!xmlHttp){
alert("AJAX服务对象创建失败!");
}
function fun()
{
var obj = document.getElementById("ddlProvince");
if(document.getElementById("ddlCity").length!=0)
{
document.getElementById("ddlCity").length=0;
}
var ddlvalue = obj.options[obj.selectedIndex].value;
if(document.getElementById("ddlCounty").length!=0)
{
document.getElementById("ddlCounty").length=0;
}
document.getElementById("ddlCounty").options.add(new Option("请选择","0"));
xmlHttp.open("GET","Default.aspx?index="+ddlvalue);
xmlHttp.onreadystatechange = result;
xmlHttp.send(null);
}
function result()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var list = xmlHttp.responseText;
if(list!="false")
{
document.getElementById("ddlCity").options.add(new Option("请选择","0"));
var arry = list.split("|");
for(var i=0;i< arry.length;i++)
{
var item = arry[i].split(",");
document.getElementById("ddlCity").options.add(new Option(item[1],item[0]));
}
}
else
{
if(document.getElementById("ddlCity").length!=0)
{
document.getElementById("ddlCity").length=0;
}
document.getElementById("ddlCity").options.add(new Option("请选择","0"));
}
}
}
}
function show()
{
var obj = document.getElementById("ddlCity");
if(document.getElementById("ddlCounty").length!=0)
{
document.getElementById("ddlCounty").length=0;
}
var str = obj.options[obj.selectedIndex].value;
xmlHttp.open("GET","Default.aspx?value="+str);
xmlHttp.onreadystatechange= returnfun;
xmlHttp.send(null);
}
function returnfun()
{
if(xmlHttp.readyState ==4)
{
if(xmlHttp.status ==200)
{
var list = xmlHttp.responseText;
if(list!="false")
{
var arry = list.split("|");
for(var i=0;i< arry.length;i++)
{
var item = arry[i].split(",");
document.getElementById("ddlCounty").options.add(new Option(item[1],item[0]));
}
}
else
{
if(document.getElementById("ddlCounty").length!=0)
{
document.getElementById("ddlCounty").length=0;
}
document.getElementById("ddlCounty").options.add(new Option("请选择","0"));
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlProvince" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlCounty" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: