Javascript 级联MVC 3中禁用的下拉列表
我在cascade中有两个下拉列表 DD2依赖于DD1 如果D1不包含所选项目的数据,如何禁用DD2Javascript 级联MVC 3中禁用的下拉列表,javascript,asp.net-mvc-3,Javascript,Asp.net Mvc 3,我在cascade中有两个下拉列表 DD2依赖于DD1 如果D1不包含所选项目的数据,如何禁用DD2 $(function () { $('#DD1').change(function () { var selected1 = $(this).val(); $.getJSON('@Url.Action("GetCiudadList", "ConsultorioSuper", new { Area = "Superusuari
$(function () {
$('#DD1').change(function () {
var selected1 = $(this).val();
$.getJSON('@Url.Action("GetCiudadList", "ConsultorioSuper", new { Area = "Superusuario", controller = "Consultorio" })', { IdDD: selected }, function (myData) {
var Select2 = $('#DD2');
Select2.empty();
$.each(myData, function (index, itemData) {
citiesSelect.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
});
})
@Html.DropDownListFor(x => x.SelectedId1, new SelectList(ViewBag.IdUniversidad, "Id1", "Name"), "-- Select --", new { id = "DD1" })
@Html.DropDownListFor(x => x.SelectedId2, new SelectList(Enumerable.Empty<SelectListItem>(), "Id2", "Name"), "-- Select --", new { id = "DD2" })
$(函数(){
$('#DD1')。更改(函数(){
var selected1=$(this.val();
$.getJSON('@Url.Action(“GetCiudadList”,“consultoriosper”,new{Area=“Superusuario”,controller=“Consultorio”})”,{IdDD:selected},函数(myData){
var Select2=$(“#DD2”);
选择2.empty();
$.each(myData,函数(索引,itemData){
CitieSelect.append($(''){
value:itemData.value,
text:itemData.text
}));
});
});
});
})
@DropDownListFor(x=>x.selected1,新选择列表(ViewBag.IdUniversidad,“Id1”,“Name”),“--Select--”,新{id=“DD1”})
@DropDownListFor(x=>x.SelectedId2,新选择列表(Enumerable.Empty(),“Id2”,“Name”),“--Select--”,新{id=“DD2”})
祝福
如果D1不包含所选项目的数据,如何禁用DD2
$(function () {
$('#DD1').change(function () {
var selected1 = $(this).val();
$.getJSON('@Url.Action("GetCiudadList", "ConsultorioSuper", new { Area = "Superusuario", controller = "Consultorio" })', { IdDD: selected }, function (myData) {
var Select2 = $('#DD2');
Select2.empty();
$.each(myData, function (index, itemData) {
citiesSelect.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
});
})
@Html.DropDownListFor(x => x.SelectedId1, new SelectList(ViewBag.IdUniversidad, "Id1", "Name"), "-- Select --", new { id = "DD1" })
@Html.DropDownListFor(x => x.SelectedId2, new SelectList(Enumerable.Empty<SelectListItem>(), "Id2", "Name"), "-- Select --", new { id = "DD2" })
您如何知道D1不包含所选项目的数据?在这种情况下,控制器操作返回什么?我猜是一个空数组。如果是这种情况,一个简单的If
条件就可以完成这项工作:
$.getJSON('@Url.Action("GetCiudadList", "ConsultorioSuper", new { Area = "Superusuario", controller = "Consultorio" })', { IdDD: selected }, function (myData) {
var Select2 = $('#DD2');
if (myData.length == null || myData.length == 0) {
// null or empty data => disable the second ddl and stop executing
// this function
Select2.attr('disabled', 'disabled');
return;
}
// at this stage we know that the myData array contains element =>
// rebind the second dropdown list with those elements
Select2.empty();
$.each(myData, function (index, itemData) {
citiesSelect.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
$.getJSON('@Url.Action(“GetCiudadList”,“consultoriosper”,new{Area=“Superusuario”,controller=“Consultorio”})”,{IdDD:selected},函数(myData){
var Select2=$(“#DD2”);
if(myData.length==null | | myData.length==0){
//null或空数据=>禁用第二个ddl并停止执行
//此函数
选择2.attr('disabled','disabled');
返回;
}
//在这个阶段,我们知道myData数组包含element=>
//用这些元素重新绑定第二个下拉列表
选择2.empty();
$.each(myData,函数(索引,itemData){
CitieSelect.append($(''){
value:itemData.value,
text:itemData.text
}));
});
});
Hello@Darin Dimitrov谢谢,但是现在,如果D1包含所选项目的数据,我如何启用DD2?Select2.removeAttr('disabled')代码>。我建议您阅读jQuery文档并阅读一些入门教程: