2012-07-08 75 views
0

我有两个级联的DropDownList已禁用级联MVC中的DropDownList 3

DD2依赖于DD1。

如果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" }) 

祝福

回答

1

我怎么禁用DD2如果D1不包含数据所选项目?

你怎么知道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 
     })); 
    }); 
}); 
+0

您好@Darin季米特洛夫谢谢,但现在,我该如何使DD2如果D1包含数据所选项目? – kalu 2012-07-09 06:51:23

+0

'Select2.removeAttr('disabled');'。我建议你朝着jQuery文档前进,并阅读一些入门教程:http://docs.jquery.com/Main_Page – 2012-07-09 06:55:16