【问题标题】:Is it possible to bind a DropDownList on OnLoad method?是否可以在 OnLoad 方法上绑定 DropDownList?
【发布时间】:2013-12-21 06:29:51
【问题描述】:

我有一个动态表,其中包含许多具有动态“id”的 DropDownList。我需要在它们被选中时绑定它们,但 OnLoad 方法不起作用。它适用于 OnClick,但不是更好的方法。

这是我的下拉菜单:

@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 下拉列表。今天它以另一种方式工作,但我需要提高性能。

有什么想法吗?

谢谢!

【问题讨论】:

  • 如果您想一次性做某事而不是后续事件,请查看jquery.one 是否有任何帮助。
  • 您需要在有人第一次使用id "reagB-" + const.Id 而不是在后续点击时运行OnLoadReagente 函数,这就是您想要的?
  • @Mathew 我这样做了:$("select").one("click", function () { alert('selected'); });,所以警报会显示一次,但每次我选择 DropDown 时它都会再次绑定。
  • 可能是因为您将该代码放在onchange 中。你在准备好的文档中这样做,看看是否有帮助。
  • @Mathew,我在document ready 中做到了,我的 Select 有一个 onclick 方法!

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


【解决方案1】:

您可以使用jquery.one 来达到您的目的,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多