Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery防止第二次按钮单击_Jquery_Html - Fatal编程技术网

使用jQuery防止第二次按钮单击

使用jQuery防止第二次按钮单击,jquery,html,Jquery,Html,我的页面上有一个按钮。如果用户再次单击按钮,我不想执行任何操作。我有以下代码。但它不起作用。有人能帮忙吗 $("#myButton").click(function(){ var count = 0; count++; alert("button"); if(count>1) $('#myButton').prop('disabled', true); }); <but

我的页面上有一个按钮。如果用户再次单击按钮,我不想执行任何操作。我有以下代码。但它不起作用。有人能帮忙吗

      $("#myButton").click(function(){
        var count = 0;
        count++;
        alert("button");
        if(count>1)
          $('#myButton').prop('disabled', true);
     });
     <button type="button" id="myButton" > Click Me </button>
$(“#我的按钮”)。单击(函数(){
var计数=0;
计数++;
警报(“按钮”);
如果(计数>1)
$('myButton').prop('disabled',true);
});
点击我
这是可以在jquery中使用的

$("#myButton").one("click",function(){
它将为最多执行一次的元素的事件附加一个处理程序。

更改此

  $("#myButton").click(function(){
      var count = 0;
      count++;
      alert("button");
      if(count>1)
          $('#myButton').prop('disabled', true);
  });
对此

  $("#myButton").one("click", function(){
      alert("button");
  });
它指定一个click事件处理程序,该处理程序仅在您第一次单击元素(在本例中为您的按钮)时触发

您可以非常小地修改现有代码以使其正常工作

  var count = 0;
  $("#myButton").click(function(){
      count++;
      alert("button");
      if(count>0)
          $('#myButton').prop('disabled', true);
  });
按照您的方式,每次单击按钮时,您都将计数设置为0(并且仅在单击两次后禁用按钮)。

使用
off()

使用
.one()
将事件绑定为运行/执行一次

 $("#myButton").one('click',function(){
    alert("button");
 });


演示:

每次单击时,您都在初始化
计数
变量。把这个移到外面,看看能不能工作

  var count = 0; 
$("#myButton").click(function(){

     count++;
    alert("button");
     if(count>0)
         $('#myButton').prop('disabled', true);
});

diastable
按钮第一次单击按钮后

检查小提琴

像这样的东西可以做到这一点

申报

  var count = 0;
从外部单击功能

检查小提琴


每次调用函数时都会初始化变量,您可以在函数外部使用变量

试试这个,不要只使用“点击”而使用“打开”,下面的代码显示了点击是如何被其替代者操纵的

 $("#myButton").on("click" , function(){

      $("#myButton").off("click"); //just off the click event once u click
        var count = 0;
         count++;
        alert("button");
        var isBool = count>1;
        $('#myButton').prop('disabled', isBool);

    });

查看此处的演示:

我在实际禁用按钮时遇到问题,尤其是在执行表单提交时。因为禁用按钮会阻止进一步的操作,所以您可以尝试在单击按钮时添加一个称为“禁用”的类,然后使用新的“禁用”类的处理程序来防止单击时的默认行为,例如:

$(".my_submit_button").click(function(e){
    $(e.target).addClass("disabled");
})

$(".disabled").click(function(e){
    e.preventDefault();
});

这应该允许按钮在第一次单击时完成任何操作(即使该操作是表单提交)但是仍然不允许任何后续的点击。

@Barmar他永远不会禁用它-他在事件处理程序中声明并分配
count=0
。一个是最好的解决方案-但是正如Barmar刚才所说,只需将
if(count>1)
更改为
if(count>0)
,这也会起作用!是的,在我看到一些最新的答案后意识到了这一点。但是,即使他将初始化移到外部,他的计数也是错误的。如果你想在第一次单击时禁用计数器,甚至不需要计数器。你只需要一个超过1秒的计数器。第二次点击就会显示警报。看看上面所有的狙击手!很高兴你得到了最多的选票@格雷厄姆里奇(GrahamRitchie)其他一半的答案(如果你按最年长的人排序,请看下面)都是同时发布的:)按最年长的人排序你排名第一——与你的快手指相比,所有其他人都不过是凡人!您可以在内部使用
$(this).unbind()
。一个
应该执行以下操作。。。有人能证实这一点吗?(我的意思是在实现方面)在答案中包含代码,而不仅仅是一个fiddle link.lol@downpoite,以获得清晰正确的答案。我甚至不知道。
Click
是一个jQuery包装器,用于
on(“Click”)
,因此没有任何区别。您每次都在定义和初始化
count
,因此它永远不会大于0。您还创建了一个布尔变量来存储函数中只使用一次的值,因此不需要它。您基本上已经将一段已经不正确的代码过度复杂化了。@Archer您怎么能说代码不正确?我建议使用on而不是click,所以没问题,我们也可以使用click and Differ来停止单击第二个tym,布尔变量返回false r true存储在varable中,这会影响按钮禁用,但不会增加第二个tym的不干净单击。现在是你的问题。过多的、不必要的代码都是100%无意义的。如果禁用了
(“单击”)
,则按钮单击事件处理程序将不再运行。每次运行代码时,您都将count声明为0,因此它永远不会大于1(这只会在代码第三次运行时发生)。这是完全不正确和误导的,应该删除。
  var count = 0;
   var count = 0;

   $("#myButton").click(function(){

      count++;

      alert("button");

      if(count>=1)

          $('#myButton').prop('disabled', true);

});
 $("#myButton").on("click" , function(){

      $("#myButton").off("click"); //just off the click event once u click
        var count = 0;
         count++;
        alert("button");
        var isBool = count>1;
        $('#myButton').prop('disabled', isBool);

    });
$(".my_submit_button").click(function(e){
    $(e.target).addClass("disabled");
})

$(".disabled").click(function(e){
    e.preventDefault();
});