[英]How to prevent click event on second times on a button
I have few buttons, when I click on those buttons some divs
are creating automatically, but here I need to prevent to create any div when I click on next time on the same button on which I already clicked. 我的按钮很少,当我单击这些按钮时,会自动创建一些
divs
,但是在这里,当我下次单击已单击的同一按钮时,需要防止创建任何div。
Code is below 代码如下
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="details">
<button id="append">button1</button>
<button id="append">button2</button>
<button id="append">button3</button>
<div id="parent"></div>
</div>
(function(){
var count = 0;
$('button').click(function(){
$('#parent').append('<div id="first'+count+'">text</div>');
count++;
});
});
Use the .one()
method. 使用
.one()
方法。 This binds a handler that only runs once for each element. 这将绑定仅对每个元素运行一次的处理程序。
$(function() { var count = 0; $('button').one("click", function() { $('#parent').append('<div id="first' + count + '">text</div>'); count++; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="details"> <button id="append">button1</button> <button id="append">button2</button> <button id="append">button3</button> <div id="parent"></div> </div>
Just unbind it after first click. 只需在第一次单击后解除绑定。
$(function(){
var count = 0;
$('button').click(function(){
count = Number($(this).data('click')||0);
if(count>0){
$(this).unbind("click");
}
count = count+1;
$(this).data('click',count);
$('#parent').append('<div id="first'+count+'">text</div>');
});
});
Basic idea is creating click flag. 基本思想是创建点击标志。 Flag is set to true once clicked and set to false if action is finished.
单击后将标志设置为true ,如果操作完成,则将标志设置为false 。 The action should be performed only if flag is false .
仅当flag为false时,才应执行该操作。
Like this: 像这样:
(function(){
var count = 0;
var flag = false;
$('button').click(function(){
if(flag) return false; // Disable when action is on
// Action starts
flag = true;
$('#parent').append('<div id="first'+count+'">text</div>');
count++;
// Action ends
flag = false;
});
});
Here are three possible solutions: 这是三种可能的解决方案:
$(this).attr('disabled',true);
$(this).attr('disabled',true);
单击的按钮$(this).attr('disabled',true);
inside anonymous click function. Adding a data-enabled attribute to the buttons and checking it. 向按钮添加启用了数据的属性并进行检查。 Your HTML for a button becomes like this:
<button id="append" data-enabled="true">button1</button>
and then you add this to the anonymous click function: 按钮的HTML变为:
<button id="append" data-enabled="true">button1</button>
,然后将其添加到匿名click函数中:
(function(){ var count = 0; $('button').click(function(){ if ( $(this).data('enabled') == 'false' ) return; else { $(this).data('enabled', 'false'); $('#parent').append('<div id="first'+count+'">text</div>'); count++; } }); });
Create an array that with the button as keys and their enabled/disabled boolean value as their value and refer to that in the anonymous click function. 创建一个以按钮为键并将其启用/禁用布尔值作为其值的数组,并在匿名click函数中引用该数组。
1- Your code needs a $
in first of your js
to calling it after document.ready
: $(function(){...})
, or adding ()
at end of it for calling Immediately ( (function(){...})();
). 1您的代码需要
$
在第一你的js
后调用它document.ready
: $(function(){...})
或增加()
在它结束调用立即( (function(){...})();
)。
2- You can use off
to remove a listener from an element. 2-您可以使用
off
从元素中删除侦听器。
other things is ok in your code. 在您的代码中其他事情还可以。 please look at result:
请看结果:
$(function(){ var count = 0; $('button').click(function(){ $('#parent').append('<div id="first'+count+'">text</div>'); count++; $(this).off("click");//One of benefits of this way is: you can add some conditions for removing this event. > if(...) $(this).off("click"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="details"> <button id="append">button1</button> <button id="append">button2</button> <button id="append">button3</button> <div id="parent"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.