click() 事件在 jquery 中调用了两次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6731894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:18:21  来源:igfitidea点击:

click() event is calling twice in jquery

jquery

提问by domlao

I setup a link element and called its click event in jquery but the click event is calling twice, please see below the code of jquery.

我设置了一个链接元素并在 jquery 中调用了它的 click 事件,但是 click 事件调用了两次,请参阅下面的 jquery 代码。

$("#link_button")
.button()
.click(function () {
   $("#attachmentForm").slideToggle("fast");
});

Please advise.

请指教。

Thanks.

谢谢。

回答by Scott

Make sure and check that you have not accidentally included your script twice in your HTML page.

确保并检查您没有在 HTML 页面中两次意外地包含您的脚本。

回答by TheSystem

Make un unbind before the click;

点击前解除绑定;

$("#link_button").unbind('click');
$("#link_button")
.button()
.click(function () {
   $("#attachmentForm").slideToggle("fast");
});

回答by Nalan Madheswaran

It means your code included the jquery script twice. But try this:

这意味着您的代码两次包含 jquery 脚本。但是试试这个:

$("#btn").unbind("click").click(function(){

//your code

});

回答by Aps18

I tried , e.stopImmediatePropagation(); This seems to work for me.

我试过了, e.stopImmediatePropagation(); 这似乎对我有用。

回答by Upendra

In my case I used the below script to overcome the issue

在我的情况下,我使用下面的脚本来解决这个问题

$('#id').off().on('click',function(){
    //...
});

off()unbinds all the events bind to #id. If you want to unbind only the clickevent, then use off('click').

off()解除绑定到的所有事件#id。如果您只想解除绑定click事件,请使用off('click').

回答by Shrinivas

Simply call .off() right before you call .on().

只需在调用 .on() 之前调用 .off()。

This will remove all event handlers:

这将删除所有事件处理程序:

$(element).off().on('click', function() {
// function body
});

To only remove registered 'click' event handlers:

仅删除已注册的“点击”事件处理程序:

$(element).off('click').on('click', function() {
// function body
});

回答by demongolem

A rather common solution to the two click problem is putting

两次点击问题的一个相当常见的解决方案是将

e.stopPropagation()

at the end of your function (assuming you use function(e)that is). This prevents the event from bubbling up which could lead to a second execution of the function.

在您的功能结束时(假设您使用function(e)它)。这可以防止事件冒泡,这可能导致函数的第二次执行。

回答by Ellington Brambila

I had the same problem, but you can try changing this code

我遇到了同样的问题,但您可以尝试更改此代码

$("#link_button")
.button()
.click(function () {
   $("#attachmentForm").slideToggle("fast");
});

to this:

对此:

$("#link_button").button();
$("#link_button").unbind("click").click(function () {
   $("#attachmentForm").slideToggle("fast");
});

For me, this code solved the problem. But take care not to accidentally include your script twice in your HTML page. I think that if you are doing these two things correctly, your code will work correctly. Thanks

对我来说,这段代码解决了这个问题。但是请注意不要在 HTML 页面中意外地两次包含您的脚本。我认为如果你正确地做这两件事,你的代码就会正常工作。谢谢

回答by Airy

This is definitely a bug specially while it's FireFox. I searched alot tried all the above answers and finally got it as bug by many experts over SO. So, I finally came up with this idea by declaring variable like

这绝对是一个错误,特别是当它是 FireFox 时。我搜索了很多尝试了上述所有答案,最终被许多专家认为是错误的。所以,我终于通过声明变量来提出这个想法

var called = false;
$("#ColorPalete li").click(function() {
    if(!called)
    {
             called = true;
             setTimeout(function(){ //<-----This can be an ajax request but keep in mind to set called=false when you get response or when the function has successfully executed.
                 alert('I am called');
                 called = false;
             },3000);

    }
});

In this way it first checks rather the function was previously called or not.

通过这种方式,它首先检查该函数之前是否被调用过。

回答by Raj

please try this

请试试这个

$('#ddlSupervisor').live('change', function (e) {
    if (e.handled !== true) { //this makes event to fire only once
    getEmployeesbySupervisor();
    e.handled = true;
   }
});