Maison > interface Web > js tutoriel > le corps du texte

Jquery实现无刷新DropDownList联动实现代码_jquery

WBOY
Libérer: 2016-05-16 18:33:06
original
972 Les gens l'ont consulté

先看HTML,我们引用Jquery,放两个DropDownList:

复制代码 代码如下:




  





接着写核心的Script:
复制代码 代码如下:



非常简单,检查值是不是0,然后ajax传值到server,成功后remove掉原来的option,append新的option.
看下WebPage的code:
复制代码 代码如下:

public partial class _Default : System.Web.UI.Page
{
[WebMethod]
public static List FetchEmployeeCars(int id)
{
var emp = new EmployeeCar();
return emp.FetchEmployeeCars(id);
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var employees = new Employee();
ddlEmployee.DataSource = employees.FetchEmployees();
ddlEmployee.DataTextField = "Surname";
ddlEmployee.DataValueField = "Id";
ddlEmployee.DataBind();
}
}
}

我们的Datasource class:
复制代码 代码如下:

public class EmployeeCar
{
public int Id { get; set; }
public string Car { get; set; }

private static List LoadData()
{
return new List
{
new EmployeeCar {Id = 1, Car = "Ford"},
new EmployeeCar {Id = 1, Car = "Holden"},
new EmployeeCar {Id = 1, Car = "Honda"},
new EmployeeCar {Id = 2, Car = "Toyota"},
new EmployeeCar {Id = 2, Car = "General Motors"},
new EmployeeCar {Id = 2, Car = "Volvo"},
new EmployeeCar {Id = 3, Car = "Ferrari"},
new EmployeeCar {Id = 3, Car = "Porsche"},
new EmployeeCar {Id = 3, Car = "Ford2"}
};
}

public List FetchEmployeeCars(int id)
{
return (from p in LoadData()
where p.Id == id
select p).ToList();
}
}

public class Employee
{
public int Id { get; set; }
public string GivenName { get; set; }
public string Surname { get; set; }

public List FetchEmployees()
{
return new List
{
new Employee {Id = 1, GivenName = "Tom", Surname = "Hanks"},
new Employee {Id = 2, GivenName = "Hugh", Surname = "Jackman"},
new Employee {Id = 3, GivenName = "Petter", Surname = "Liu"}
};
}

public Employee FetchEmployee(int id)
{
var employees = FetchEmployees();
return (from p in employees
where p.Id == id
select p).First();
}
}

完了。希望这篇POST对您有帮助。
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal