我有一个包含两个@Html.DropDownListFor
助手的视图:
<div class="editor-field">
@Html.DropDownListFor(model => model.Employee, (IEnumerable<SelectListItem>)ViewBag.emps, "--Select--", new { style = "width:150px", id = "ddl1"})
@Html.ValidationMessageFor(model => model.Employee)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Employee2, (IEnumerable<SelectListItem>)ViewBag.emps, "--Select--", new { style = "width:150px", id = "ddl2"})
@Html.ValidationMessageFor(model => model.Employee2)
</div>
它们的填充方式如下:
public ActionResult Create()
{
List<Employees> emps = new List<Employees>();
emps.Add(new Employees { Id = 0, Name = "Michael Jordan" });
emps.Add(new Employees { Id = 1, Name = "Magic Johnson" });
emps.Add(new Employees { Id = 2, Name = "Larry Bird" });
var items = emps.Select(i => new SelectListItem
{
Value= i.Id.ToString(),
Text =i.Name
});
ViewBag.emps = items;
return View();
}
如何从第二个 DDL 中删除第一个 DDL 的选定项?我设法使用这样的jQuery获取选定的项目:
<script type="text/javascript">
$(function () {
$("#ddl1").change(function () {
alert($("#ddl1").val());
});
});
</script>
但是,我找不到将它用于我的目的的方法。