前端界面
<div>
<div>
<select onchange="GetWd(1)" id="province" name="province">
<option>--请选择--</option>
</select>
<select onchange="GetWd(2)" id="city" name="city">
<option>--请选择--</option>
</select>
<select onchange="GetWd(3)" id="area" name="area">
<option>--请选择--</option>
</select>
</div>
<div id="city_box">
</div>
</div>
数据库字段值
js部分
<script>
$.ajax({
type: "post",
url: "/dev/GetCity.ashx",
data: {
action: 'city',
num: 0,
},
success: function(d) {
var dd = JSON.parse(d);
$.each(dd.data, function(i, o) {
$('#province').append('<option value="' + o.sheng + '">' + o.sheng + '</option>');
});
}
})
function GetWd(num) {
if(num == 1) {
var province = $('#province').val();
if(province != '') {
$.ajax({
type: "post",
url: "/dev/GetCity.ashx",
data: {
action: 'city',
num: num,
province: province,
},
success: function(d) {
var dd = JSON.parse(d);
document.getElementById('city').innerHTML = '<option>--请选择--</option>';
$.each(dd.data, function(i, o) {
$('#city').append('<option value="' + o.shi + '">' + o.shi + '</option>');
});
$('#city_box').html('');
$.each(dd.datas, function(i, o) {
$('#city_box').append('<h2>地址:' + o.address + '<br>电话:' + o.tel + '</h2>')
});
}
})
}
document.getElementById('city').innerHTML = '<option>--请选择--</option>';
document.getElementById('area').innerHTML = '<option>--请选择--</option>';
}
if(num == 2) {
var province = $('#province').val();
var city = $('#city').val();
if(province != '') {
if(city != '') {
$.ajax({
type: "post",
url: "/dev/GetCity.ashx",
data: {
action: 'city',
num: num,
province: province,
city: city,
},
success: function(d) {
var dd = JSON.parse(d);
document.getElementById('area').innerHTML = '<option>--请选择--</option>';
$.each(dd.data, function(i, o) {
$('#area').append('<option value="' + o.qu + '">' + o.qu + '</option>');
});
$('#city_box').html('');
$.each(dd.datas, function(i, o) {
$('#city_box').append('<h2>地址:' + o.address + '<br>电话:' + o.tel + '</h2>')
});
}
})
}
document.getElementById('area').innerHTML = '<option>--请选择--</option>';
}
}
if(num == 3) {
var province = $('#province').val();
var city = $('#city').val();
var area = $('#area').val();
if(province != '') {
if(city != '') {
if(area != '') {
$.ajax({
type: "post",
url: "/dev/GetCity.ashx",
data: {
action: 'city',
num: num,
province: province,
city: city,
area: area
},
success: function(d) {
var dd = JSON.parse(d);
$('#city_box').html('');
$.each(dd.data, function(i, o) {
$('#city_box').append('<h2>地址:' + o.address + '<br>电话:' + o.tel + '</h2>')
});
}
})
}
}
}
}
}
</script>
执行程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string action = context.Request["action"];
switch (action)
{
case "city":
GetWd(context);
break;
}
}
public void GetWd(HttpContext context)
{
int num = int.Parse(context.Request["num"].ToString());
string province = context.Request["province"];
string city = context.Request["city"];
string area = context.Request["area"];
string sqlStr = "";
DataTable dt;
if (num == 0)
{
sqlStr = "select sheng from nw_customform_user_fuwuwangdian group by sheng";
dt = SqlEasy.ExecuteDataTable(sqlStr);
context.Response.Write("{\"status\":\"0\",\"data\":" + JSONhelper.ToJson(dt, false) + "}");
}
if (num == 1)
{
sqlStr = "select shi from nw_customform_user_fuwuwangdian where sheng='" + province + "' group by shi";
dt = SqlEasy.ExecuteDataTable(sqlStr);
DataTable dts = SqlEasy.ExecuteDataTable("select * from nw_customform_user_fuwuwangdian where sheng='" + province + "'");
context.Response.Write("{\"status\":\"0\",\"data\":" + JSONhelper.ToJson(dt, false) + ",\"datas\":" + JSONhelper.ToJson(dts, false) + "}");
}
if (num == 2)
{
sqlStr = "select qu from nw_customform_user_fuwuwangdian where sheng='" + province + "' and shi='" + city + "' group by qu";
dt = SqlEasy.ExecuteDataTable(sqlStr);
DataTable dts = SqlEasy.ExecuteDataTable("select * from nw_customform_user_fuwuwangdian where sheng='" + province + "' and shi='" + city + "'");
context.Response.Write("{\"status\":\"0\",\"data\":" + JSONhelper.ToJson(dt, false) + ",\"datas\":" + JSONhelper.ToJson(dts, false) + "}");
}
if (num == 3)
{
sqlStr = "select * from nw_customform_user_fuwuwangdian where sheng='" + province + "' and shi='" + city + "' and qu='" + area + "'";
dt = SqlEasy.ExecuteDataTable(sqlStr);
context.Response.Write("{\"status\":\"0\",\"data\":" + JSONhelper.ToJson(dt, false) + "}");
}
}
页面效果