admin管理员组文章数量:1417472
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
2 Answers
Reset to default 4One 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.
本文标签: javascriptDo not bind an event handler if has been already boundStack Overflow
版权声明:本文标题:javascript - Do not bind an event handler if has been already bound - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745267582a2650695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论