开发者

Using jQuery .live to populate a dropdown and selecting an item

开发者 https://www.devze.com 2023-01-29 17:53 出处:网络
See here if you want to see what I am doing. I can get a dropdrop list to load from a JSON source, but when I select an item the list reloads again. I know what the problem is, I just don\'t know the

See here if you want to see what I am doing.

I can get a dropdrop list to load from a JSON source, but when I select an item the list reloads again. I know what the problem is, I just don't know the solution.

$("#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);
});

I know the problem is "click", but I don't know what I should use instead.

Update new related problem The only problem I have now is when the edit page loads, I have to reselect my data in each drop down. How do I get the drop down to load when I load the page?

Working Code thus far minus problem above

The display

<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>

And the script

// 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 += ">" +开发者_JAVA百科 item.Title + "</option>";
        };
    });
    $(this).html(items);
});

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


Try 'focus' or 'change', maybe.

0

精彩评论

暂无评论...
验证码 换一张
取 消