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

2016-02-19 13:32 10 1 收藏

有了下面这个Jquery实现无刷新DropDownList联动实现代码教程,不懂Jquery实现无刷新DropDownList联动实现代码的也能装懂了,赶紧get起来装逼一下吧!

【 tulaoshi.com - Web开发 】

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

代码如下:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)
style type="text/css"
#ddlEmployeeCars
{
display:none;
position:absolute;
top:50px;
left:9px;
}
/style
script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"/script

   asp:DropDownList runat="server" AppendDataBoundItems="true"
asp:ListItem Text="(Please Select)" Value="0" Selected="True" /
/asp:DropDownList
asp:DropDownList runat="server"
/asp:DropDownList

接着写核心的Script:

代码如下:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)
script language="javascript" type="text/javascript"
$(function() {
var $ddl = $("select[name$=ddlEmployee]");
var $ddlCars = $("select[name$=ddlEmployeeCars]");
$ddl.focus();
$ddl.bind("change keyup", function() {
if ($(this).val() != "0") {
loadEmployeeCars($("select option:selected").val());
$ddlCars.fadeIn("slow");
} else {
$ddlCars.fadeOut("slow");
}
});
});

function loadEmployeeCars(selectedItem) {
$.ajax({
type: "POST",
url: "Default.aspx/FetchEmployeeCars",
data: "{id: " + selectedItem + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function Success(data) {
printEmployeeCars(data.d);
}
});
}

function printEmployeeCars(data) {
$("select[name$=ddlEmployeeCars] option").remove();
for (var i = 0; i data.length; i++) {
$("select[name$=ddlEmployeeCars]").append(
$("option/option").val(data[i].Id).html(data[i].Car)
);
}
}
/script

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

代码如下:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)
public partial class _Default : System.Web.UI.Page
{
[WebMethod]
public static ListEmployeeCar 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:

代码如下:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)
public class EmployeeCar
{
public int Id { get; set; }
public string Car { get; set; }

private static ListEmployeeCar LoadData()
{
return new ListEmployeeCar
{
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 ListEmployeeCar 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 ListEmployee FetchEmployees()
{
return new ListEmployee
{
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对您有帮助。

来源:http://www.tulaoshi.com/n/20160219/1604567.html

延伸阅读
标签: Web开发
代码如下: script type="text/javascript" src="js/jquery.min.js"/script script type="text/javascript" $(function(){ $("li").hover(function(){ $(this).addClass("ho"); }, function(){ $(this).removeClass("ho"); }); $("li").click(function(){ $(this).removeClass("ho").addClass("xiaoshi").siblings().removeClass("x...
标签: Web开发
HTML HEAD titleAjax实现无刷新三联动下拉框/title meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR" meta content="C#" name="CODE_LANGUAGE" meta content="JavaScript" name="vs_defaultClientScript" meta content="http://schemas.microsoft.com/intellis...
标签: Web开发
打包下载 查了一下jq的官方插件编写文档( http://docs.jquery.com/Plugins/Authoring )以及文档中推荐的Mike Alsup写的一篇 A Plugin Development Pattern 。英语不是很好,但还是努力看下来(既学习到知识又能练习英语,何乐不为),照猫画虎的写了一个处女作——tabBox。 顾名思义,这个插件就是方便的产生具有tab选项卡功能“盒子...
标签: Web开发
基于jquery的锁定弹出层 这个东西也是随手总结出来的,引用了一些js框架jquery的方法。 div遮盖其他控件的方法参考了 对于需要遮盖flash的,请将flash控件的WMode变量值设置为Transparent 使用方法: 代码如下: script src="jquery.js"/script script src="effect/maskDiv.js"/script script //配置模块 var moduleEvent = [{"i...
标签: Web开发
多选效果如下图 代码如下: 代码如下: script type="text/javascript"!-- $(document).ready(function() { $("#selectall").click(selectAll); }); function selectAll() { var checked = $("#selectall").attr("checked"); $(".selectable").each(function() { var subchecked = $(this).attr("checked"); if (subchecked != ch...

经验教程

624

收藏

23
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部