无刷新省市三联动(源码)
***************************************html页*******************************************************
<title></title>
<style type="text/css">
#city,#area{display:none;margin-left:10px;}
select{float:left};
</style>
<script src="jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "post",
contentType: "application/json",
url: "WebService2.asmx/GetProv",
data: "{}",
success: function (result) {
var strprov;
for (var i = 0; i < result.d.length; i++) {
var str = result.d[i];
strprov += "<option value='" + str.split('-')[0] + "'>" + str.split('-')[1] + "</option>";
}
$('#province').append(strprov);
}
})
$('#province').change(function () {
$('#city').css('display', 'block');
$('#area').css('display', 'none');
$('#city option:gt(0)').remove();
$('#area option:gt(0)').remove();
$.ajax({
type: "post",
contentType: "application/json",
url: "WebService2.asmx/GetCity",
data: "{citystr:" + $(this).val() + "}",
success: function (result) {
var strprov;
for (var i = 0; i < result.d.length; i++) {
var str = result.d[i];
strprov += "<option value='" + str.split('-')[0] + "'>" + str.split('-')[1] + "</option>";
}
$('#city').append(strprov);
}
})
})
$('#city').change(function () {
$('#area').css('display', 'block');
$('#area option:gt(0)').remove();
$.ajax({
type: "post",
contentType: "application/json",
url: "WebService2.asmx/GetArea",
data: "{areastr:" + $(this).val() + "}",
success: function (result) {
var strprov;
for (var i = 0; i < result.d.length; i++) {
var str = result.d[i];
strprov += "<option value='" + str.split('-')[0] + "'>" + str.split('-')[1] + "</option>";
}
$('#area').append(strprov);
}
})
})
})
</script>
</head>
<body>
<select id="province">
<option>----请选择省----</option>
</select>
<select id="city">
<option>----请选择市----</option>
</select>
<select id="area">
<option>----请选择县----</option>
</select>
</body>
*************************************WebService2.asmx*****************************************
string connectionString = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public List<string> GetProv()
{
List<string> strlist = new List<string>();
string connectionString = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select provinceID,provincename FROM province";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
foreach (DataRow row in dt.Rows)
{
strlist.Add(row["provinceID"].ToString() + "-" + row["provincename"]);
}
return strlist;
}
[WebMethod]
public List<string> GetCity(string citystr)
{
List<string> strlist = new List<string>();
string connectionString = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select cityID,cityname FROM city WHERE father=@citystr";
cmd.Parameters.AddWithValue("@citystr", citystr);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
foreach (DataRow row in dt.Rows)
{
strlist.Add(row["cityID"].ToString() + "-" + row["cityname"].ToString());
}
return strlist;
}
[WebMethod]
public List<string> GetArea(string areastr)
{
List<string> strlist = new List<string>();
string connectionString = ConfigurationManager.ConnectionStrings["sqlservercon"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select areaID,areaname FROM area WHERE father=@areastr";
cmd.Parameters.AddWithValue("@areastr", areastr);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
foreach (DataRow row in dt.Rows)
{
strlist.Add(row["areaID"].ToString() + "-" + row["areaname"].ToString());
}
return strlist;
}