Jquery实现无刷新DropDownList联动
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<style type="text/css">
#ddlEmployeeCars
{
display: none;
position: absolute;
top: 50px;
left: 9px;
}
</style>
<script 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: "WebForm1.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) {
/* 方法1
$("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) );
}
*/
$("select[name$=ddlEmployeeCars]").empty();
$.each(data, function (i, item) {
$("<option></option>").val(item["Id"]).text(item["Car"]).appendTo($("select[name$=ddlEmployeeCars]"));
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlEmployee" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="(Please Select)" Value="0" Selected="True" />
</asp:DropDownList>
<asp:DropDownList ID="ddlEmployeeCars" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
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();
}
}
[WebMethod]
public static List<EmployeeCar> FetchEmployeeCars(int id)
{
var emp = new EmployeeCar();
return emp.FetchEmployeeCars(id);
}
public class EmployeeCar
{
public int Id { get; set; }
public string Car { get; set; }
private static List<EmployeeCar> LoadData()
{
return new List<EmployeeCar>
{
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<EmployeeCar> 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<Employee> FetchEmployees()
{
return new List<Employee>
{
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();
}
}
Jquery实现无刷新DropDownList联动的更多相关文章
- jquery 无刷新多级联动
原先不熟悉jquery时,总在寻找无刷新的方法,在此不断的积累自己所知道的jquery属性,常用方法.以下为jquery实现的无刷新联动事件 分公司: <select id="Sele ...
- JQuery中国省市区无刷新三级联动查询
之前有写过用<Ajax控件来实现中国的省市区无刷新查询> 今天用JQuery来实现,用Ajax控件和JQuery的优缺点就先不说了. 效果图如下: 下面来结合代码来详细说明一下如何用JQu ...
- JQuery实现无刷新下拉加载图片
最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...
- jQuery.pager无刷新分页
刚刚学习前端的时候,需要一个无刷新的分页功能,找了一个不错的,大家也有很大分享,在这里写一个自己的部分代码,前后端都有,需要的小伙伴可以参考一下,代码不是完整的. 直接上伪代码<样式代码省略,部 ...
- jQuery Ajax无刷新操作
下面是“无刷新登录”的例子,采用Ashx+jQuery Ajax实现. //后台实例代码 ashx文件(可替换为从数据库中读取) public void ProcessRequest(HttpCont ...
- jQuery实现无刷新切换主题皮肤功能
主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...
- jQuery实现两个DropDownList联动(MVC)
近段时间原本是学习MVC的,谁知道把jQuery也学上了.而且觉得对jQuery更感兴趣,比如今早上有写了一个练习<jQuery实现DropDownList(MVC)>http://www ...
- JQUERY AJAX无刷新异步上传文件
AJAX无刷新上传文件并显示 http://blog.csdn.net/gao3705512/article/details/9330637?utm_source=tuicool jQuery For ...
- jquery.axios无刷新机制删除
思路:无刷新机制就是不用的刷新动作 ,用前端html语法删除和后端的数据库删,同时删除达到效果 除操作,来实现无刷洗的方法
随机推荐
- MOSFET shunt regulator substitutes for series regulator
You would normally use a series linear regulator or a dc/dc converter to obtain 3V dc from a higher ...
- MYSQL SELECT 过程 转
本文从一个select语句的执行过程出发, 遍历MySQL的多个几子系统. 先放图一张, 按图索骥开始我们的历险. <ignore_js_op> 当客户端连接上MySQL服务端之后 ...
- Spring事务为什么不会自动回滚?Spring事务怎样才会自动回滚?事务自动回滚条件及手动回滚
原文:https://blog.csdn.net/qq_32331073/article/details/76508147 更多Spring事务问题请访问链接:Spring事务回滚问题疑难详解 在此, ...
- AE 打开工具箱工具的对话框
The best approach to run a system tool—or any tool or model created, and saved in a toolbox from a b ...
- windows服务与计划任务
1. 打开Visual Studio 2012新建一个project Solution: 2. 选择Windows->windows Service,修改服务名称:MyFirstService: ...
- [Linux] git send-email的使用
1. git send-email is included in an individual package, named "git-email":$ sudo apt-get i ...
- MapReduce实战--倒排索引
本文地址:http://www.cnblogs.com/archimedes/p/mapreduce-inverted-index.html,转载请注明源地址. 1.倒排索引简介 倒排索引(Inver ...
- hdu5297 Y sequence(容斥原理+迭代)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5297 题意:给定整数n和整数r,在1.2.3.4.5.......的序列中删掉能够开2次方的数,3次方的数 ...
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- easyui window refresh 刷新两次解决办法
这样写刷新两次 $("#windowid").window('refresh','url01.php'); $("#windowid").window('ope ...