Instance code of ajax.net + jquery three-level linkage

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! 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> </title>
<Script src = "Jquery1.7.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
$. Ajax ({
Type: "post ",
ContentType: "application/json ",
Url: "WebService1.asmx/loadprovince ",
Data :"{}",
Success: function (result ){
Var ping;
For (var I = 0; I <result. d. length; I ++ ){
Ping + = "<option value =" + result. d [I]. provinceID + "> ";
Ping + = result. d [I]. provincename;
Ping + = "</option> ";

}
$ ('# Select1'). append (ping );


}


})
$ ('# Select1'). change (function (){
// Remember to clear the contents of city and county during the second selection
$ ('# Select2 option: gt (0)'). remove ();
$ ('# Select3 option: gt (0)'). remove ();
// Bind the next list to the province change event (the province id is to be lost)

$. Ajax ({
Type: "post ",
ContentType: "application/json ",
Url: "WebService1.asmx/loadcity ",
Data: "{fatherid: '" + $ (' # Select1 option: selected'). val () + "'}",
Success: function (result ){
Var str2;
For (var I = 0; I <result. d. length; I ++ ){
Str2 + = "<option value =" + result. d [I]. cityID + "> ";
Str2 + = result. d [I]. cityname;
Str2 + = "</option> ";
}

$ ('# Select2'). append (str2 );
}
})
})
$ ('# Select2'). change (function (){
$ ('# Select3 option: gt (0)'). remove ();

$. Ajax ({
Type: "post ",
ContentType: "application/json ",
Url: "WebService1.asmx/loadarea ",
Data: "{fatherid: '" + $ (' # Select2 option: selected'). val () + "'}",
Success: function (result ){
Var str3;
For (var I = 0; I <result. d. length; I ++ ){
Str3 + = "<option value =" + result. d. areaID + "> ";
Str3 + = result. d [I]. areaname;
Str3 + = result. d [I]. father;

}
$ ('# Select3'). append (str3 );
}
})
})
})

</Script>
</Head>
<Body>
Save:
<Select id = "Select1">
<Option> -- select -- </option>
</Select>
City:
<Select id = "Select2">
<Option> -- select -- </option>
</Select>
County:
<Select id = "Select3">
<Option> -- select -- </option>
</Select>
</Body>
</Html>

Webservice:

Copy codeThe Code is as follows:
[WebMethod] // load Province
Public List <Model. province> loadprovince () // Why do I need to use list? Because the previous value is shown here, it is impossible for one instance to be multiple model instances. One instance is a record to prevent field errors.
{
BLL. province bp = new BLL. province ();
List <Model. province> list = bp. getlistModel ();
Return list;
}
[WebMethod] // load the city
Public List <Model. city> loadcity (string fatherid)
{
BLL. city bc = new BLL. city ();
List <Model. city> list = bc. getlistmodel (fatherid );
Return list;
}
[WebMethod] // load County
Public List <Model. area> loadarea (string fatherid)
{
BLL. area ba = new BLL. area ();
List <Model. area> list = ba. getlistmodel (fatherid );
Return list;

}
}
}

DAL -- area

Copy codeThe Code is as follows:
Public System. Collections. Generic. List <Model. area> getlistmodel (string fatherid)
{
System. Collections. Generic. List <Model. area> list = new System. Collections. Generic. List <Model. area> ();
DataTable dt = GetList ("father =" + fatherid + ""). Tables [0];
Foreach (DataRow row in dt. Rows)
{
Model. area ma = new Model. area ();
Ma. areaID = row ["areaID"]. ToString ();
Ma. areaname = row ["areaname"]. ToString ();
Ma. father = row ["father"]. ToString ();
List. Add (ma );
}
Return list;
}


Dal -- city

Copy codeThe Code is as follows:
Public System. Collections. Generic. List <Model. area> getlistmodel (string fatherid)
{
System. Collections. Generic. List <Model. area> list = new System. Collections. Generic. List <Model. area> ();
DataTable dt = GetList ("father =" + fatherid + ""). Tables [0];
Foreach (DataRow row in dt. Rows)
{
Model. area ma = new Model. area ();
Ma. areaID = row ["areaID"]. ToString ();
Ma. areaname = row ["areaname"]. ToString ();
Ma. father = row ["father"]. ToString ();
List. Add (ma );
}
Return list;
}
}


DAL-provience

Copy codeThe Code is as follows:
Public System. Collections. Generic. List <Model. province> getlistModel ()
{
// The content to be queried is returned as an instance
// What we need to do here is to create a list and fill the list with the model instance, while the model needs to use a method to lose data and add it to the model.
// Create a list instance
System. Collections. Generic. List <Model. province> list = new System. Collections. Generic. List <Model. province> ();
// If you have lost data, you don't have to write it yourself. You can call the Getlist method to manipulate the database to get data.
DataTable dt = GetList (""). Tables [0];
// After obtaining the data, you need to add the data to the model instance.

Foreach (DataRow row in dt. Rows)
{
// Each row is an instance, so we need to put the model in the loop.
Model. province mp = new Model. province ();
Mp. provinceID = row ["provinceID"]. ToString ();
Mp. provincename = row ["provincename"]. ToString ();
List. Add (mp); // put all instances not added to the list
}
Return list;


}

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.