Jquery 是否可以在OnLoad方法上绑定DropDownList?
我有一个动态表,其中包含许多具有动态“ID”的DropDownList。我需要在选择它们时绑定它们,但OnLoad方法不起作用。它与OnClick一起工作,但这不是更好的方法 这是我的下拉列表:Jquery 是否可以在OnLoad方法上绑定DropDownList?,jquery,asp.net-mvc,razor,html-select,Jquery,Asp.net Mvc,Razor,Html Select,我有一个动态表,其中包含许多具有动态“ID”的DropDownList。我需要在选择它们时绑定它们,但OnLoad方法不起作用。它与OnClick一起工作,但这不是更好的方法 这是我的下拉列表: @Html.DropDownList("reagB-" + const.Id, new SelectList(new List<Reagente>(), "Id ", "Nome"), Resources.Geral.Selecione, new { onchange = "Selecion
@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));" })
@Html.DropDownList(“reagB-”+const.Id,new SelectList(new List(),“Id”,“Nome”),Resources.Geral.Selecione,new{onchange=“selecioreagente($(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>");
}
});
}
})
单击里根特(cbo,里根特ID)的函数{
var gcId=cbo.attr(“id”).split('-')[1];
$(“#reagB-”+gcId+“选项”).remove();
$.ajax({
url:“/Grupo/ComboReagentes”,
数据类型:“json”,
类型:“POST”,
数据:{
GrupoConstitutionId:gcId,
Equipmentoid:cbo.val()
},
成功:功能(数据){
var cboReagente=$(“#reagB-”+gcId);
$。每个(数据、功能(i、项){
如果(item.Id==ReagenteId&&ReagenteId>0){
cboreagent.append(“+item.Nome+”);
}否则{
cboreagent.append(“+item.Nome+”);
}
});
}
})
}
当我选择下拉菜单时,会调用OnClick,但每次我选择下拉菜单时都会调用OnClick。我需要的是只在第一次单击时加载de下拉列表。今天它以另一种方式工作,但我需要改进性能
有什么想法吗
谢谢 您可以根据自己的目的使用
$(function(){
$(".dropdown-class").one("click",function(){//call the method you specified in onclick here})
});
如果你想做一件事,而不是在后续事件中,看看是否有帮助。当有人第一次用
id
“reagB-”+const.id单击Select
时,你需要运行OnLoadReagente
功能,而不是在后续的单击中,这就是你想要的吗?@Mathew我这样做了:$(“选择”).one(“单击”),函数(){alert('selected');})代码>,因此警报显示一次,但每次我选择下拉列表时,它都会再次绑定。可能是因为您将该代码放入了onchange
。您可以在document ready中这样做,看看是否有帮助。@Mathew,我是在document ready
中这样做的,我的Select有一个onclick方法!