How to realize the three-stage linkage without refresh by ajax.net + jquery
This article mainly introduces ajax.net + jquery how to achieve non-refresh three-level linkage, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The code is as follows:
$(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; iTunes +) {
Ping + = ""
Ping + = result.d [I] .provincename
Ping + = ""
}
$('# Select1') .append (ping)
}
})
$('# Select1') .change (function () {
/ / remember to clear the contents of cities and counties in the second election.
$('# Select2 option:gt (0)'). Remove ()
$('# Select3 option:gt (0)'). Remove ()
/ / bind the next down list in the provincial change event (to lose the provincial id)
$.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; iTunes +) {
Str2 + = ""
Str2 + = result.d [I] .cityname
Str2 + = ""
}
$('# 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; iTunes +) {
Str3 + = ""
Str3 + = result.d [I] .areaname
Str3 + = result.d [I] .resume
}
$('# Select3') .append (str3)
}
})
})
})
Province:
Please select--
City:
Please select--
County:
Please select--
Webservice:
The copy code is as follows:
[WebMethod] / / loading province
Public List loadprovince () / / Why do you use list because it is impossible to use the previous value here? one instance is multiple model instances, and one instance is a record to prevent field errors.
{
BLL.province bp = new BLL.province ()
List list=bp.getlistModel ()
Return list
}
[WebMethod] / / loading city
Public List loadcity (string fatherid)
{
BLL.city bc = new BLL.city ()
List list = bc.getlistmodel (fatherid)
Return list
}
[WebMethod] / / loading county
Public List loadarea (string fatherid)
{
BLL.area ba = new BLL.area ()
List list = ba.getlistmodel (fatherid)
Return list
}
}
}
DAL--area
The copy code is as follows:
Public System.Collections.Generic.List getlistmodel (string fatherid)
{
System.Collections.Generic.List list = new System.Collections.Generic.List ()
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
The copy code is as follows:
Public System.Collections.Generic.List getlistmodel (string fatherid)
{
System.Collections.Generic.List list = new System.Collections.Generic.List ()
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
The copy code is as follows:
Public System.Collections.Generic.List getlistModel ()
{
/ / the content to be checked is returned as an example
/ / what we need to do here is to create the list and fill the list with the model instance, while the model uses a method to lose the data and add it to the model
/ / build a list instance
System.Collections.Generic.List list = new System.Collections.Generic.List ()
/ / if you already have a method to lose data, you don't have to write it yourself. You can get the data by calling Getlist method to manipulate the database.
DataTable dt = GetList ("") .Tables [0]
/ / after getting the data, you need to add the data to the model instance.
Foreach (DataRow row in dt.Rows)
{
/ / each line is an instance, so put model's in the loop
Model.province mp = new Model.province ()
Mp.provinceID = row ["provinceID"] .ToString ()
Mp.provincename = row ["provincename"] .ToString ()
List.Add (mp); / / add an instance to the list before it is added
}
Return list
}
Thank you for reading this article carefully. I hope the article "ajax.net + jquery how to achieve non-refreshing three-level linkage" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!