using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Pull_down_List
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
CityDal dal = new CityDal();
[WebMethod]
public List<City> SelectCity(int Pid)
{
return dal.SelectCity(Pid);
}
}
}
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<h2>多级联动</h2>
<div class="form-inline">
<div class="row">
<div class="col-lg-12">
<select class="form-control" id="select1" onchange="load2()">
<option value="value">-请选择-</option>
</select>
<select class="form-control" id="select2" onchange="load3()">
<option value="value">-请选择-</option>
</select>
<select class="form-control" id="select3">
<option value="value">-请选择-</option>
</select>
</div>
</div>
</div>
<script>
$(function () {
load1(0);
})
function load1(pid) {
var o = { Pid: pid };
$.ajax({
url: "https://localhost:44328/WebService1.asmx/SelectCity",
data: JSON.stringify(o),
type: "post",
dataType: "json",
contentType: "application/json",
success: function (data) {
$(data.d).each(function () {
$("#select1").append('<option value=" ' + this.ID + '">' + this.Name + '</option>');
})
}
})
}
function load2() {
var pid = $("#select1").val();
var o = { Pid: pid };
$("#select2").empty();
$.ajax({
url: "https://localhost:44328/WebService1.asmx/SelectCity",
data: JSON.stringify(o),
type: "post",
dataType: "json",
contentType: "application/json",
success: function (data) {
$(data.d).each(function () {
$("#select2").append('<option value=" ' + this.ID + '">' + this.Name + '</option>');
})
}
})
}
function load3() {
var pid = $("#select2").val();
var o = { Pid: pid };
$("#select3").empty();
$.ajax({
url: "https://localhost:44328/WebService1.asmx/SelectCity",
data: JSON.stringify(o),
type: "post",
dataType: "json",
contentType: "application/json",
success: function (data) {
$(data.d).each(function () {
$("#select3").append('<option value=" ' + this.ID + '">' + this.Name + '</option>');
})
}
})
}
</script>