是否可以在OnLoad方法上绑定DropDownList?

时间:2013-12-04 16:40:36

标签: jquery asp.net-mvc razor html-select

我有一个带有许多DropDownLists的动态表,它们具有动态'ids'。我需要在选中它们时将它们绑定,但OnLoad方法不起作用。它适用于OnClick,但它不是更好的方法。

这是我的DropDown:

@Html.DropDownList("reagB-" + const.Id, new SelectList(new List<Reagente>(), "Id ", "Nome"), Resources.Geral.Selecione, new { onchange = "SelecionaReagente($(this));", onclick = "OnClickReagente($(this), " + (resultado != null ? resultado.ReagenteId : 0) + ");", onLoad = "OnLoadReagente($(this));" })

这是我的jquery代码:

function OnClickReagente(cbo, ReagenteId) {
var gcId = cbo.attr("id").split('-')[1];

$("#reagB-" + gcId + " option").remove();

$.ajax({
    url: "/Grupo/ComboReagentes",
    dataType: 'json',
    type: "POST",
    data: {
        grupoConstituinteId: gcId,
        equipamentoId: cbo.val()
    },
    success: function (data) {
        var cboReagente = $("#reagB-" + gcId);

        $.each(data, function (i, item) {
            if (item.Id == ReagenteId && ReagenteId > 0) {
                cboReagente.append("<option value='" + item.Id + " selected='selected''>" + item.Nome + "</option>");
            } else {
                cboReagente.append("<option value='" + item.Id + "'>" + item.Nome + "</option>");
            }
        });                        
    }
})

}

当我选择下拉列表时,会调用OnClick,但每次选择下拉列表时都会发生。我需要的是只在第一次点击时加载de dropdown。今天它以另一种方式工作,但我需要提高性能。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将jquery.one用于您的目的,

$(function(){ 
       $(".dropdown-class").one("click",function(){//call the method you specified in onclick here}) 
});
相关问题