javascript - trigger only once in document click - Stack Overflow

I have this script$(document).on("click", "#ocs", function(){alert("ASdasd&quo

I have this script

$(document).on("click", "#ocs", function(){
    alert("ASdasd");
});

but on a unknown reasons, it trigger the click twice. I can surely use

$("#ocs").unbind().click(function(){
    alert("ASdasd");
});

to make it not trigger twice but for my requirements I don't use

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

so any ideas how to do unbind() or not to make trigger twice using

$(document).on("click", "selector", function(){

?

I have this script

$(document).on("click", "#ocs", function(){
    alert("ASdasd");
});

but on a unknown reasons, it trigger the click twice. I can surely use

$("#ocs").unbind().click(function(){
    alert("ASdasd");
});

to make it not trigger twice but for my requirements I don't use

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

so any ideas how to do unbind() or not to make trigger twice using

$(document).on("click", "selector", function(){

?

Share Improve this question asked Aug 4, 2015 at 5:24 Juliver GalletoJuliver Galleto 9,05727 gold badges92 silver badges169 bronze badges 6
  • 1 Can you show some HTML? It should not be triggering twice in the first place – AmmarCSE Commented Aug 4, 2015 at 5:25
  • Its a system, very heavy html. – Juliver Galleto Commented Aug 4, 2015 at 5:30
  • 3 Well, its either the handler is attached twice, or you have two elements with the same id – AmmarCSE Commented Aug 4, 2015 at 5:31
  • Did you set that inside a JS function? E.g. function store() { $(document).on("click", "#ocs", function(){ ... }) } – Robin Carlo Catacutan Commented Aug 4, 2015 at 5:33
  • @AmmarCSE: ill take not of that, ill check that attached twice and same id stuffs, thank you – Juliver Galleto Commented Aug 4, 2015 at 5:39
 |  Show 1 more ment

2 Answers 2

Reset to default 9

you can use one for that

$(document).one("click", "#ocs", function(){
    alert("ASdasd");
});

You can do this way:

$('#abc').bind('click', function() {
   $(this).unbind('click');
   alert('Clicked and Unbind');

});

The bind does allow you to re-bind the click event later if needed.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745337374a4623167.html

相关推荐

  • javascript - trigger only once in document click - Stack Overflow

    I have this script$(document).on("click", "#ocs", function(){alert("ASdasd&quo

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信