最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do not bind an event handler if has been already bound - Stack Overflow

programmeradmin1浏览0评论

I am calling some forms using ajax, and then binding an event handler for every button. The problem is... when I call a new form using ajax the event handler is called again for the new elements and then it's added twice for the previous elements. How can I detect if an event handler is already on an element and not bind it again?

function x(){
    $("input:button").click(function(){
        alert('is this called once?');
    });
}

<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

function x(){
    $("input:button").click(function(){
        alert('is this called once?');
    });
}

// calling event twice simulating an ajax calling:
x();
x();
<script src=".11.1/jquery.min.js"></script>
<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

I am calling some forms using ajax, and then binding an event handler for every button. The problem is... when I call a new form using ajax the event handler is called again for the new elements and then it's added twice for the previous elements. How can I detect if an event handler is already on an element and not bind it again?

function x(){
    $("input:button").click(function(){
        alert('is this called once?');
    });
}

<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

function x(){
    $("input:button").click(function(){
        alert('is this called once?');
    });
}

// calling event twice simulating an ajax calling:
x();
x();
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

Share Improve this question edited Jun 21, 2016 at 23:40 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Jun 21, 2016 at 22:31 straminstramin 2,4003 gold badges32 silver badges66 bronze badges 1
  • 3 Use event delegation so that you only have to set up the event handler once. – Pointy Commented Jun 21, 2016 at 22:35
Add a ment  | 

2 Answers 2

Reset to default 4

One way to do this is use off function of jQuery to remove any event attached and then attach it.

This is make sure there is only one click event attached to element.

Example Snippet:

function x() {
  $("input:button").off('click').on('click', function() {
    console.log('is this called once?');
  });
}

// calling event twice simulating an ajax calling:
x();
x();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

Does the click handler actually need to be re-added every time your AJAX request is called? If not, consider revising your code like so:

//Only call this once; new buttons will still trigger it
$(document).on('click', 'input:button', function() {
    alert('is this called once?');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' value='Disable me' />
<input type='button' value='Disable me' />

By attaching the handler to the document and supplying a selector ('input:button'), the function only triggers when clicking on buttons, but will automatically apply to any new buttons that are added after the initial binding.

发布评论

评论列表(0)

  1. 暂无评论