2013-07-29 27 views
-1

我有javasctipt可變如何在視圖中訪問js變量,Html.DropDownList?

var orgCode = ""; 
     $("#orgs").change(function() { 
      var org = document.getElementById("orgs"); 
      orgCode = org.options[org.selectedIndex].value; 

     }); 

我想通過orgCode我的DropDownList所有的時間,當變量的變化:

@Html.DropDownListFor(model => model.Doctor, new SelectList(_unitOfWork.Doctors.GetByOrganization(orgCode).ToList(), "Code", "Name"), new { id = "docs" }) 

orgCode總是變化的,但我怎麼能訪問它的DropDownList並每次刷新它變化的變化?

在此先感謝!

+0

?什麼是$(「#orgs」)? – Nithesh

+0

請看看這裏:http://stackoverflow.com/questions/4160735/mvc-dropdownlist-capturing-change-event-without-attaching-the-event-handler –

+0

Nithesh,$(「#orgs」)是另一個下拉列表,我從 –

回答

0

每當變量發生變化時,您都可以爲下拉菜單設置選定的值:ie;

var orgCode = ""; 
$("#orgs").change(function() { 
    var org = document.getElementById("orgs"); 
    orgCode = org.options[org.selectedIndex].value; 
    $('#docs').val(orgCode); 
}); 
1

嘗試這樣的事情

$("#orgs").change(function() { 
     var orgCode = $("#orgs").val(); 
     $.ajax({ 
      url:"controler/action", // which returns Dotors list as json 
      data:{org_Code:orgCode} ,//Passing orgCode to the above action method 
      type:"json", 
      success:function(data){ 
      $('#docs').find('option').remove(); 
      foreach(var d in data){ 
       $('<option>').val(d["code"]).text(d["Name"]).appendTo('#docs'); 
      } 
      } 
}); 
要綁定的變化,新的代碼
相關問題