Ajax级联菜单实例代码
1.ajax.html
<!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>
<title>ajax</title>
<script type="text/javascript">
function loadxmldoc(txt) {
var xmlhttp;
if (window.xmlhttprequest) {// code for ie7+, firefox, chrome, opera, safari
xmlhttp = new xmlhttprequest();
}
else {// code for ie6, ie5
xmlhttp = new activexobject("microsoft.xmlhttp");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
var citys = xmlhttp.responsetext.split(',');
document.getelementbyid("citys").length = 1;
for (i = 0; i < citys.length - 1; i++) {
document.getelementbyid("citys").add(new option(citys[i], citys[i]));
}
// document.getelementbyid("mydiv").innerhtml = xmlhttp.responsetext;
}
}
xmlhttp.open("get","ajax/getdata.aspx?pro="+txt, true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>ajax</h2>
<select id="select1" onchange="loadxmldoc(this.value)">
<option>请选择省份</option>
<option value="1">江苏</option>
<option value="2">上海</option>
</select>
<select id="citys">
<option>请选择城市</option>
</select>
<div id="mydiv"></div>
</body>
</html>
2.getdata.aspx.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class ajax_getdata : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
string[] js=new string[]{"南京", "苏州", "常州" , "无锡" , "镇江"};
string pro=request.querystring["pro"];
if (pro == "1")
{
string temp = "";
for (int i = 0; i < js.length; i++)
{
temp = temp + js[i];
temp = temp + ",";
}
response.write(temp);
}
else
response.write("");
}
}
上一篇: 牛奶是寒性食物吗
下一篇: 宝宝辅食搭配 哪些辅食不能一起吃