简体   繁体   English

如何防止第二次单击按钮上的单击事件

[英]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 代码如下

HTML 的HTML

<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>

SCRIPT 脚本

(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: 这是三种可能的解决方案:

  1. Disable the button that was clicked by adding $(this).attr('disabled',true); 通过添加$(this).attr('disabled',true);单击的按钮$(this).attr('disabled',true); inside anonymous click function. 内部匿名点击功能。
  2. 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++; } }); }); 
  3. 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.

相关问题 如何防止第二次单击单选按钮(如果已选中),从而可以防止javascript事件 - How to prevent second click on radio button if it is already checked so that javascript event can be prevented 防止jQuery中的click事件多次触发 - Prevent click event in jQuery triggering multiple times 如何在事件仍在发生时防止按钮单击? Javascript - How to prevent button click while event still happening? Javascript 如何使用Vue指令防止链接或按钮中的点击事件 - How to prevent click event in link or button using Vue directive 如何防止GWT中的按钮单击事件重新加载html代码 - how to prevent html code reload on button click event in GWT jQuery:button_click事件在第一次单击时不触发,而在第二次单击时起作用,为什么? 如何解决? - jQuery : button_click event not fires on first click and works on second click, why? How to fix it? JSF提交按钮 - 表单在一秒钟内被提交X次(直到视图重新创建) - 如何防止它? - JSF submit button - form is X times submited during one second (until view recreated) - how to prevent it? 每秒单击一次按钮事件 - SImple click a button event every second 阻止按钮单击事件以刷新页面 - Prevent a button click event to refresh the page 使用 Click Outside 挂钩时,如何防止将事件侦听器多次分配给元素? - How to prevent assigning an event listener multiple times to an element when using Click Outside hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM