使用jQuery .live来填充一个下拉列表并选择一个项目 [英] Using jQuery .live to populate a dropdown and selecting an item

查看:109
本文介绍了使用jQuery .live来填充一个下拉列表并选择一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此处 if你想看看我在做什么。

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

我可以得到一个从JSON源加载的dropdrop列表,但是当我选择一个项目时,列表重新加载。我知道问题是什么,我只是不知道解决方案。

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?

到目前为止的工作代码

显示

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


推荐答案

尝试'focus'或'change'

Try 'focus' or 'change', maybe.

这篇关于使用jQuery .live来填充一个下拉列表并选择一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆