jQuery:单击按钮,单击事件触发两次或多次 [英] jQuery: Single button click, click event firing two or more times

查看:605
本文介绍了jQuery:单击按钮,单击事件触发两次或多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HTML输入和按钮元素。我使用按钮提交表单,打开辅助窗口等。问题是,单击将变成2(有时甚至更多)表单提交,或打开两个额外的浏览器窗口。我已经在多个网络浏览器中重现了这个问题。我已经尝试切换jQuery版本,并没有改变任何东西。什么可能导致这种事情发生?

解决方案

您可能会发生问题,因为您将多个相同的处理程序分配给点击事件。我建议你检查你分配处理程序的行不是无意中被多次调用。另一个解决方案可能是这样的一个取消绑定的呼叫:

  $(#myButton)。unbind(click) 。点击(将myHandler); 

但是没有看到我只是猜测的代码。


I have HTML input and button elements. I use the buttons to submit forms, open secondary windows, etc. The problem is, a single click is turning into 2 (and sometimes many more) form submissions, or opening two additional browser windows. I've reproduced the problem in multiple web browsers. I've tried switching jQuery versions and that didn't change anything. What could be causing this sort of thing to happen?

解决方案

Your problem may be happening because you are assigning the same handler to the click event multiple times. I suggest you check that the line where you assign the handler is not being called multiple times inadvertently. Another solution could be a call to unbind first like this:

$("#myButton").unbind("click").click(myHandler);

But without seeing some code I'm just guessing.

这篇关于jQuery:单击按钮,单击事件触发两次或多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆