Ajax and WebService to achieve the provincial and county level three linkage specific code _ajax related

Source: Internet
Author: User
-------------------------------------Webservice1.asmx---------------------------------------
Copy Code code as follows:

To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
public class WebService1:System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
[WebMethod]
Public list<model.province> getprovince ()
{
Bll.province Bpro = new Bll.province ();
list<model.province> list = Bpro. Getlistmodel ();
return list;
}
[WebMethod]
Public list<model.city> Getcitybypro (string proid)
{
Bll.city bcity = new bll.city ();
list<model.city> list = bcity. Getlistmodel ("father= '" + proid + "");
return list;
}
[WebMethod]
Public list<model.area> getareabycity (string Cityid)
{
Bll.area Barea = new Bll.area ();
list<model.area> list = Barea. Getlistmodel ("father= '" + Cityid + "");
return list;
}

----------------------------------htmlpage1.htm----------------------------
Copy Code code as follows:

<title></title>
<style type= "Text/css" >
Select
{
width:150px;
}
</style>
<script src= "Js/jquery1.7.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/getprovince",
Data: "{}",
Success:function (Result) {
var stroption = ';
for (var i = 0; i < result.d.length; i++) {
Stroption + = ' <option value= ' + Result.d[i].provinceid + ' > ';
Stroption + = Result.d[i].provincename;
Stroption + = ' </option> ';
}
$ (' #seprovince '). Append (stroption);
}
})
$ (' #seprovince '). Change (function () {
$ (' #secity option:gt (0) '). Remove ();
$ (' #searea option:gt (0) '). Remove ();
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/getcitybypro",
Data: "{proid: '" + $ (This). Val () + "'}",
Success:function (Result) {
var strocity = ';
for (var i = 0; i < result.d.length; i++) {
Strocity + = ' <option value= ' + Result.d[i].cityid + ' > ';
Strocity + = Result.d[i].cityname;
Strocity + = ' </option> ';
}
$ (' #secity '). Append (strocity);
}
})
})
$ (' #secity '). Change (function () {
$ (' #searea option:gt (0) '). Remove ();
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/getareabycity",
Data: "{cityid: '" + $ (This). Val () + "'}",
Success:function (Result) {
var stroarea = ';
for (var i = 0; i < result.d.length; i++) {
Stroarea + = ' <option value= ' + result.d[i].areaid + ' > ';
Stroarea + = Result.d[i].areaname;
Stroarea + = ' </option> ';
}
$ (' #searea '). Append (Stroarea);
}
})
})
})
</script>
<body>
<table>
<tr>
<td>
Address
</td>
<td>
<select id= "Seprovince" >
<option>--Please select--</option>
</select>
Province
<select id= "Secity" >
<option>--Please select--</option>
</select> City
<select id= "Searea" >
<option>--Please select--</option>
</select> County
</td>
</tr>
</table>
</body>


Note: The three-tier architecture is used, and the DAL layer writes some methods
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.