【发布时间】: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