如何使用javascript启用onclick事件?

时间:2015-11-21 06:03:37

标签: javascript html

我使用以下代码禁用onclick事件。 现在的代码

function moveToDeckTable(game_table_id,category_id,status){
    var_game_table_id=game_table_id;
    var redirect="table.php";
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "ahr/addPlayer.php",
        data: {game_table_id:game_table_id,status:status},
        cache: false,
        success: function(data){
            if(data.success==1){
                PlayingStatus =setInterval(setPlayingStatus,1000);
                open_game_window = window.open(redirect+"?id="+data.game_table_id,"","_blank");
            }    
            else if(data.success==0)
                alert("User already playing..");
            else if(data.success==2)
                alert("You have not enough coins to play in this table .buy more coins or play in minor coins table.");    
        }
    }); 
    //code to disable onclick

}
function setPlayingStatus(){
    if(open_game_window.closed){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "ahr/setPlayingStatus.php",
            cache: false,
            success: function(data)
            {
                clearInterval(PlayingStatus);
            }
        }); 
        //code to re enable onclick
    }    
}

现在,我想在另一个按钮点击时再次启用该事件。如何启用onclick事件?

3 个答案:

答案 0 :(得分:2)

检查一下。它不需要您取消或重新初始化"事件。



window.onload = function() {
  //capture the click event
  var eventsEnabled = true;

  var log = document.getElementById('log');

  document.getElementById('button').onclick = function() {
    //if capturing events is true
    if (eventsEnabled == true) {
      log.innerHTML = 'event captured';
    }
  };



  document.getElementById('captureStop').onclick = function() {
    eventsEnabled = false;
    log.innerHTML = 'capturing turned off';
  };

  document.getElementById('captureStart').onclick = function() {
    eventsEnabled = true;
    log.innerHTML = 'capturing turned on';
  };
};

<input type="button" id="button" value="This is a button">
<input type="button" id="captureStop" value="Stop the evil clicking (capturing)">
<input type="button" id="captureStart" value="Start the evil clicking (capturing)">
<div id="log"></div>
&#13;
&#13;
&#13;

<强>更新 我真的没办法测试这段代码,所以你必须告诉我它是否有效。你/我将从那里改进它并结束这个问题。

这是您现有的一些代码。我们需要切换禁用功能。看起来很简单。只需调整我的&#34; old&#34;中的代码。为此目的回答功能。

function moveToDeckTable(game_table_id,category_id,status){
    if(captureEvents == true){//your if (see my first answer [above])
    var_game_table_id=game_table_id;
    var redirect="table.php";
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "ahr/addPlayer.php",
        data: {game_table_id:game_table_id,status:status},
        cache: false,
        success: function(data){
            if(data.success==1){
                PlayingStatus =setInterval(setPlayingStatus,1000);
                open_game_window = window.open(redirect+"?id="+data.game_table_id,"","_blank");
            }    
            else if(data.success==0)
                alert("User already playing..");
            else if(data.success==2)
                alert("You have not enough coins to play in this table .buy more coins or play in minor coins table.");    
        }
    }); 
    //code to disable onclik
  }
}
function setPlayingStatus(){
    if(captureEvents == false){
    if(open_game_window.closed){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "ahr/setPlayingStatus.php",
            cache: false,
            success: function(data)
            {
                clearInterval(PlayingStatus);
            }
        }); 
        //code to re enable onclik
    }  
   }   
}

答案 1 :(得分:0)

function enable(){
$( "body" ).delegate( '#btn'+game_table_id+' > .green_btn', "click", function() {
// your code
});
}

onClick其他函数调用启用函数,它为按钮添加事件

答案 2 :(得分:0)

工作代码。 @ www139感谢更新的代码。 function1-disable the onclick if captureEvents true

function2-enable the onclick if captureEvents false