I have the following problem with this code:
我对这个代码有以下问题:
<button id="delete">Remove items</button>
$("#delete").button({
icons: {
primary: 'ui-icon-trash'
}
}).click(function() {
alert("Clicked");
});
If I click this button, the alert show up two times. It's not only with this specific button but with every single button I create.
如果我点击这个按钮,警报会出现两次。不仅仅是这个特定的按钮,还有我创建的每个按钮。
What am I doing wrong?
我做错了什么?
8 个解决方案
#1
57
Your current code works, you can try it here: http://jsfiddle.net/s4UyH/
您当前的代码可以在这里使用:http://jsfiddle.net/s4UyH/
You have something outside the example triggering another .click()
, check for other handlers that are also triggering a click
event on that element.
在示例之外触发另一个.click(),检查是否有其他处理程序也在该元素上触发单击事件。
#2
97
In that case, we can do the following
在这种情况下,我们可以执行以下操作
$('selected').unbind('click').bind('click', function (e) {
do_something();
});
I had the event firing two times initially, when the page get refreshed it fires four times. It was after many fruitless hours before I figured out with a google search.
最初,我让事件触发了两次,当页面刷新时,它触发了四次。经过了几个小时的无果之后,我才想出了谷歌搜索。
I must also say that the code initially was working until I started using the JQueryUI accordion widget.
我还必须指出,在开始使用JQueryUI accordion小部件之前,代码最初是有效的。
#3
25
Strange behaviour which I was experienced also. So for me "return false" did the trick.
我也经历过这种奇怪的行为。所以对我来说,“返回假”就是这样。
$( '#selector' ).on( 'click', function() {
//code
return false;
});
#4
14
If you use
如果你使用
$( document ).ready({ })
or
或
$(function() { });
more than once, the click function will trigger as many times as it is used.
不止一次,单击函数将触发多次使用。
#5
7
you can try this.
你可以试试这个。
$('#id').off().on('click', function() {
// function body
});
$('.class').off().on('click', function() {
// function body
});
#6
2
I had the same problem and tried everything but it didn't worked. So I used following trick:
我遇到了同样的问题,尝试了所有的方法,但都没有成功。我用了下面的技巧
function do_stuff(e)
{
if(e){ alert(e); }
}
$("#delete").click(function() {
do_stuff("Clicked");
});
You check if that parameter isn't null than you do code. So when the function will triggered second time it will show what you want.
检查该参数是否为空,而不是代码。所以当函数第二次触发时它会显示你想要的。
#7
1
I've found that binding an element.click in a function that happens more than once will queue it so next time you click it, it will trigger as many times as the binding function was executed. Newcomer mistake probably on my end but I hope it helps. TL,DR: Make sure you bind all clicks on a setup function that only happens once.
我发现绑定一个元素。单击发生不止一次的函数将会对其进行排队,因此下次单击它时,将触发与绑定函数相同的多次执行。新来的人可能会犯错,但我希望这能有所帮助。TL,DR:确保您绑定了设置函数上的所有单击,而设置函数只发生一次。
#8
-1
Just like what Nick is trying to say, something from outside is triggering the event twice. To solve that you should use event.stopPropagation() to prevent the parent element from bubbling.
就像尼克想说的那样,外部的一些东西触发了两次事件。为了解决这个问题,您应该使用event.stopPropagation()来防止父元素冒泡。
$('button').click(function(event) {
event.stopPropagation();
});
I hope this helps.
我希望这可以帮助。
#1
57
Your current code works, you can try it here: http://jsfiddle.net/s4UyH/
您当前的代码可以在这里使用:http://jsfiddle.net/s4UyH/
You have something outside the example triggering another .click()
, check for other handlers that are also triggering a click
event on that element.
在示例之外触发另一个.click(),检查是否有其他处理程序也在该元素上触发单击事件。
#2
97
In that case, we can do the following
在这种情况下,我们可以执行以下操作
$('selected').unbind('click').bind('click', function (e) {
do_something();
});
I had the event firing two times initially, when the page get refreshed it fires four times. It was after many fruitless hours before I figured out with a google search.
最初,我让事件触发了两次,当页面刷新时,它触发了四次。经过了几个小时的无果之后,我才想出了谷歌搜索。
I must also say that the code initially was working until I started using the JQueryUI accordion widget.
我还必须指出,在开始使用JQueryUI accordion小部件之前,代码最初是有效的。
#3
25
Strange behaviour which I was experienced also. So for me "return false" did the trick.
我也经历过这种奇怪的行为。所以对我来说,“返回假”就是这样。
$( '#selector' ).on( 'click', function() {
//code
return false;
});
#4
14
If you use
如果你使用
$( document ).ready({ })
or
或
$(function() { });
more than once, the click function will trigger as many times as it is used.
不止一次,单击函数将触发多次使用。
#5
7
you can try this.
你可以试试这个。
$('#id').off().on('click', function() {
// function body
});
$('.class').off().on('click', function() {
// function body
});
#6
2
I had the same problem and tried everything but it didn't worked. So I used following trick:
我遇到了同样的问题,尝试了所有的方法,但都没有成功。我用了下面的技巧
function do_stuff(e)
{
if(e){ alert(e); }
}
$("#delete").click(function() {
do_stuff("Clicked");
});
You check if that parameter isn't null than you do code. So when the function will triggered second time it will show what you want.
检查该参数是否为空,而不是代码。所以当函数第二次触发时它会显示你想要的。
#7
1
I've found that binding an element.click in a function that happens more than once will queue it so next time you click it, it will trigger as many times as the binding function was executed. Newcomer mistake probably on my end but I hope it helps. TL,DR: Make sure you bind all clicks on a setup function that only happens once.
我发现绑定一个元素。单击发生不止一次的函数将会对其进行排队,因此下次单击它时,将触发与绑定函数相同的多次执行。新来的人可能会犯错,但我希望这能有所帮助。TL,DR:确保您绑定了设置函数上的所有单击,而设置函数只发生一次。
#8
-1
Just like what Nick is trying to say, something from outside is triggering the event twice. To solve that you should use event.stopPropagation() to prevent the parent element from bubbling.
就像尼克想说的那样,外部的一些东西触发了两次事件。为了解决这个问题,您应该使用event.stopPropagation()来防止父元素冒泡。
$('button').click(function(event) {
event.stopPropagation();
});
I hope this helps.
我希望这可以帮助。