2013-12-12 211 views
1

我有一个可点击的按钮列表,并使用标志变量,以防止双击焦点按钮。该标志起作用,我收到预期的警报,说'你已经点击了'。问题是,下面的按钮将被视为已被点击,我会再次得到警报。我不想要这个。可点击按钮表的麻烦,需要防止双击聚焦按钮,同时保持以下可点击

var _clickFlag = true; 

    /* #Volunteers is a table. Each row on the table has a button that says "accept", 
I pass the function the 'event' object which I use to get specific data from that table 
row and send it to a database*/ 

    $('#Volunteers').on('click','#accept', function(event){ 

     //if clickFlag = true then the button hasn't been clicked yet. 

     if(_clickFlag){ 

      //here I send some stuff to a database. I don't want to send it twice for the same 
      //row, which is why I need to prevent a double click 

      //set clickFlag to false to prevent double submission 

       _clickFlag = false; 



    }else{ 

    //alert if the button has been clicked once already 

     alert("already accepted"); 
    } 


    }); 
+0

应该在你的代码中的错误,你可能正在使用相同的ID不止一个你纽扣。 –

回答

1

双击是一个单独的事件,我假设你说的是多次点击按钮,如果是你的变量,你定义的外部上下文是共享给所有按钮,你需要的是定义一个变量的函数中,并把它绑按钮本身,请尝试以下:

http://jsfiddle.net/4JFtw/

$('#Volunteers').on('click','.accept', function(event){ 

    //if clickFlag = true then the button hasn't been clicked yet. 

    if(typeof this._clickFlag != 'undefined' && this._clickFlag){ 

     //alert if the button has been clicked once already 
     alert("already accepted"); 

}else{ 

    //here I send some stuff to a database. I don't want to send it twice for the same 
    //row, which is why I need to prevent a double click 
    alert('_clickFlag: '+this._clickFlag + ' First time processing!'); 
    this._clickFlag = true; 

} 


}); 
2

只要使用此:

$("#Volunteers").unbind("click"); 

这样只会使按钮无法点击。

+0

而现在大多数人更喜欢.off()而不是.unbind函数。它取决于你想要使用什么。 – douweegbertje

+0

如果我使用解除绑定,它将使所有的按钮无用。我只想点击禁用。 – Spilot

0

在这种情况下,我喜欢使用属性,例如:

$('some-button-here).attr('data-active','false') // This sets the attribute for that button 

就可以检查这个属性,每次一个按钮被点击。