我试图在Javascript中编写一个视频扑克游戏,作为一种获得它的基础知识的方式,我遇到了一个问题,jQuery点击事件处理程序被多次触发。

They're attached to buttons for placing a bet, and it works fine for placing a bet on the first hand during a game (firing only once); but in betting for the second hand, it fires the click event twice each time a bet or place bet button is pressed (so twice the correct amount is bet for each press). Overall, it follows this pattern for number of times the click event is fired when pressing a bet button once--where the ith term of the sequence is for the betting of the ith hand from the beginning of the game: 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, which appears to be n(n+1)/2 + 1 for whatever that's worth--and I wasn't smart enough to figure that out, I used OEIS. :)

这是一个点击事件处理程序的函数;希望这很容易理解(如果不容易,请告诉我,我也想在这方面做得更好):

/** The following function keeps track of bet buttons that are pressed, until place button is pressed to place bet. **/
function pushingBetButtons() {
    $("#money").text("Money left: $" + player.money); // displays money player has left

    $(".bet").click(function() {
        var amount = 0; // holds the amount of money the player bet on this click
        if($(this).attr("id") == "bet1") { // the player just bet $1
            amount = 1;
        } else if($(this).attr("id") == "bet5") { // etc.
            amount = 5;
        } else if($(this).attr("id") == "bet25") {
            amount = 25;
        } else if($(this).attr("id") == "bet100") {
            amount = 100;
        } else if($(this).attr("id") == "bet500") {
            amount = 500;
        } else if($(this).attr("id") == "bet1000") {
            amount = 1000;
        }
        if(player.money >= amount) { // check whether the player has this much to bet
            player.bet += amount; // add what was just bet by clicking that button to the total bet on this hand
            player.money -= amount; // and, of course, subtract it from player's current pot
            $("#money").text("Money left: $" + player.money); // then redisplay what the player has left
        } else {
            alert("You don't have $" + amount + " to bet.");
        }
    });

    $("#place").click(function() {
        if(player.bet == 0) { // player didn't bet anything on this hand
            alert("Please place a bet first.");
        } else {
            $("#card_para").css("display", "block"); // now show the cards
            $(".card").bind("click", cardClicked); // and set up the event handler for the cards
            $("#bet_buttons_para").css("display", "none"); // hide the bet buttons and place bet button
            $("#redraw").css("display", "block"); // and reshow the button for redrawing the hand
            player.bet = 0; // reset the bet for betting on the next hand
            drawNewHand(); // draw the cards
        }
    });
}

如果你有任何想法或建议,或者我的问题的解决方案与这里的另一个问题的解决方案相似,请告诉我(我看过许多类似标题的帖子,但没有幸运地找到一个适合我的解决方案)。


当前回答

发生这种情况是因为特定事件多次绑定到同一元素。

对我有效的解决方案是:

使用.die()方法杀死所有附加的事件。

然后附加方法监听器。

因此,

$('.arrow').click(function() {
// FUNCTION BODY HERE
}

应该是:

$('.arrow').die("click")
$('.arrow').click(function() {
// FUNCTION BODY HERE
}

其他回答

如果你这样写:

$(文档).off(“点击”)。on("click", "#btnID", function(){})

然后,它将禁用所有项目的单击事件。

如果您只是想关闭单击特定的按钮,请执行此操作

美元(文件)。关掉(“点击”、“# btnID”)。on(点击,btnID,功能)

https://jsfiddle.net/0vgchj9n/1/

为了确保事件总是只触发一次,可以使用Jquery .one()。JQuery可以确保你的事件处理程序只被调用一次。此外,您还可以订阅一个事件处理程序,以便在完成当前单击操作的处理后允许进一步单击。

<div id="testDiv">
  <button class="testClass">Test Button</button>
</div>

var subscribeClickEvent = function() {$("#testDiv").one("click", ".testClass", clickHandler);};

function clickHandler() {
  //... perform the tasks  
  alert("you clicked the button");
  //... subscribe the click handler again when the processing of current click operation is complete  
  subscribeClickEvent();
}

subscribeClickEvent();

Unbind()有效,但将来可能会导致其他问题。处理程序在另一个处理程序内部时会触发多次,所以让你的处理程序在外部,如果你想要嵌套的处理程序的值,将它们分配给一个全局变量,它将被你的处理程序访问。

.在页面的生命周期中只触发一次

因此,如果您想进行验证,这不是正确的解决方案,因为当您在验证后不离开页面时,您就永远不会回来。更好地使用

$(".bet").on('click',function() 
{ //validation 
   if (validated) { 
      $(".bet").off('click'); //prevent to fire again when we are not yet off the page
      //go somewhere
    }
});

如果您在每次“单击”时调用该函数,那么它将在每次调用时添加另一对处理程序。

使用jQuery添加处理程序并不像设置“onclick”属性的值。可以根据需要添加任意数量的处理程序。