2010-12-10 38 views
1

如果您想查看我在做什麼,請參閱here使用jQuery .live來填充下拉菜單並選擇一個項目

我可以從JSON源獲取一個dropdrop列表,但是當我選擇一個項目時,列表會重新加載。我知道問題是什麼,我只是不知道解決方案。

$("#RequestType").live("click", function() { 
    var items = "<option selected>(Select)</option>"; 
    $.each(jsonRequestType, function(i, item) { 
     items += "<option value='" + item.Id + "'>" + item.Title + "</option>"; 
    }); 
    $("#RequestType").html(items); 
}); 

我知道問題是「點擊」,但我不知道我應該用什麼來代替。

更新新相關問題我現在唯一的問題是編輯頁面加載時,我必須在每個下拉列表中重新選擇我的數據。當我加載頁面時,如何讓下拉菜單加載?

工作守則迄今減去以上問題

顯示

<td><%= Html.Hidden("RequestType", Model.DayRequested.RequestType, new { Class = "RequestTypeValue" })%> 
    <%= Html.DropDownList("RequestTypeDdl", new List<SelectListItem> { new SelectListItem { Text = "(Select)", Value = "" } }, new { Class = "RequestTypeDdl" })%></td> 

和腳本

// Get the request types for the drop down 
$(".RequestTypeDdl").live("focus", function() { 
    var items = "<option>(Select)</option>"; 
    var field = $(this); 
    $.each(jsonRequestType, function(i, item) { 
     items += "<option value='" + item.Id + "'"; 
      if ($(field).prev("input").val() == item.Id) 
       items += " selected"; 
      items += ">" + item.Title + "</option>"; 
     }; 
    }); 
    $(this).html(items); 
}); 

$(".RequestTypeDdl").live("change", function() { 
    $(this).prev("input").val($(this).val()); 
}); 
+0

um?期望的結果是什麼? – nickf 2010-12-10 17:01:09

回答

0

嘗試 '專注' 或 '變',也許。

+0

焦點通過「更改」組合來保存值。 – 2010-12-10 18:05:22

相關問題