在jquery中单击按钮时调用两次事件
我在单击时有一个提交按钮,我首先调用javascript中的单击函数在jquery中单击按钮时调用两次事件,jquery,Jquery,我在单击时有一个提交按钮,我首先调用javascript中的单击函数 var id = $('input[id$=chkSubcontracting]').attr("id"); var indexserverIdPrefix = id.indexOf("chkSubcontracting"); var serverId= id.substring(0,indexserverIdPrefix); $('#'+ serverId +'historyControl_historySubmitButt
var id = $('input[id$=chkSubcontracting]').attr("id");
var indexserverIdPrefix = id.indexOf("chkSubcontracting");
var serverId= id.substring(0,indexserverIdPrefix);
$('#'+ serverId +'historyControl_historySubmitButton').click(function() {
//other part of code
//if error is there then add the error message to message
if (message != "") {
$('#'+ serverId +'ErrorMessagePanelHiddenField').val("1");
$(this.ErrorMessagePanelHiddenField).val("1");
$("[id*='ErrorMessagePanelHiddenField']").val("1");
this.IsValid = 1;
alert(message);
}
else
{
$("[id*='ErrorMessagePanelHiddenField']").val("0");
}
我的Aspx页面代码在这里
<asp:Panel ID="ErrorMessagePanel" CssClass="ErrorPanel" Visible="true" runat="server">
<asp:HiddenField ID="ErrorMessagePanelHiddenField" runat="server" />
<div class="ErrorDiv" id="ErrorMessageDiv">
<asp:BulletedList CssClass="ErrorMessage" ID="ErrorMessageBulletedList" runat="server">
</asp:BulletedList>
</div>
</asp:Panel>
有时单击按钮会触发两次此事件。我还可以第一次设置“ErrorMessagePanelHiddenField”值。但不是在以后的时间。如果对元素多次调用jQuery
。单击()
,事件处理程序将被附加多次。请确保在页面中不会多次执行附加了click事件处理程序的脚本。看
另一种方法是使用jQuery的
.off()
和.on()
方法,如中所述。本质上,.off()
方法将删除任何附加的事件处理程序,因此可以确保在调用.on()
附加单击处理程序时,它将是唯一附加的事件处理程序 请输入您的html标记是否总是两次,或者每次单击“提交”按钮时您的警报计数是否都在增加?@arkantos Its不总是。但是,我无法第二次设置hiddenfield。