2010-10-03 153 views
0

我有一个包含一些下拉菜单这样的MVC观点:加载页面上的运行代码?

 <%: Html.DropDownListFor(model => model.CS.C1, Model.CS.Category1List, "-- Välj kategori --")%> 
     <%: Html.DropDownListFor(model => model.CS.C2, Model.CS.Category2List, "-- Välj kategori --")%> 
     <%: Html.DropDownListFor(model => model.CS.C3, Model.CS.Category3List, "-- Välj kategori --")%> 
     <%: Html.DropDownListFor(model => model.CS.C4, Model.CS.Category4List, "-- Välj kategori --")%> 
     <%: Html.DropDownListFor(model => model.CS.C5, Model.CS.Category5List, "-- Välj kategori --")%> 
     <%: Html.DropDownListFor(model => model.CS.C6, Model.CS.Category6List, "-- Välj kategori --")%> 

要使用AJAX恭维这个电话(创建CascadingDropDown)我有以下的javascript:

$(document).ready(function() { 

      $("#CS_C1").change(function() { lastCategoryId = $("#CS_C1").val() }); 
      $("#CS_C2").change(function() { lastCategoryId = $("#CS_C2").val() }); 
      $("#CS_C3").change(function() { lastCategoryId = $("#CS_C3").val() }); 
      $("#CS_C4").change(function() { lastCategoryId = $("#CS_C4").val() }); 
      $("#CS_C5").change(function() { lastCategoryId = $("#CS_C5").val() }); 
      $("#CS_C6").change(function() { lastCategoryId = $("#CS_C6").val() }); 

      $("#CS_C2").CascadingDropDownCategory("#CS_C1", '/AdCategory/GetCategoriesByParent', '', true, SetFilter); 
      $("#CS_C3").CascadingDropDownCategory("#CS_C2", '/AdCategory/GetCategoriesByParent', '', true, SetFilter); 
      $("#CS_C4").CascadingDropDownCategory("#CS_C3", '/AdCategory/GetCategoriesByParent', '', true, SetFilter); 
      $("#CS_C5").CascadingDropDownCategory("#CS_C4", '/AdCategory/GetCategoriesByParent', '', true, SetFilter); 
      $("#CS_C6").CascadingDropDownCategory("#CS_C5", '/AdCategory/GetCategoriesByParent', '', true, SetFilter); 

      $("#LS_L2").CascadingDropDown("#LS_L1", '/Location/GetLocationsByParent', '', true); 

      alert(lastCategoryId); 
      SetFilter(lastCategoryId); 
     }); 

的CascadingDropDownCategory功能是从this site

当执行后的下拉菜单设置properply但在运行时

SetFilter(lastCategoryId); 

的lastCategoryId是梁-1?我可以看到,下拉菜单有价值,也应该引发变化事件?为什么我得到-1?我怎么解决它?

BestRegards

+0

有这个东西叫做循环... – Skilldrick 2010-10-03 11:33:19

回答

1

lastCategoryId.change()回调设置(这意味着它不会设置直到<select>变化),所以在最后你得到你的文件准备功能的结束也不会是尚未设置。

另外,无论您声明一个没有var的变量,它都会变成全局的。在函数开始时总是用var声明变量。

+0

谢谢!因此应在更改事件之后设置SetFilter。 – Banshee 2010-10-04 13:25:12

相关问题