我试图在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
        }
    });
}

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


当前回答

因为加价,我遇到了一个问题。

HTML:

<div class="myclass">
 <div class="inner">

  <div class="myclass">
   <a href="#">Click Me</a>
  </div>

 </div>
</div>

jQuery

$('.myclass').on('click', 'a', function(event) { ... } );

你注意到我在html中有两次相同的类'myclass',所以它为每个div实例调用click。

其他回答

在我的案例中,我使用的是“委托”,所以这些解决方案都不起作用。我相信这是按钮出现多次通过ajax调用,导致多次点击问题。解决方案是使用超时,所以只有最后一次点击被识别:

var t;
$('body').delegate( '.mybutton', 'click', function(){
    // clear the timeout
    clearTimeout(t);
    // Delay the actionable script by 500ms
    t = setTimeout( function(){
        // do something here
    },500)
})

.unbind()已弃用,您应该使用.off()方法代替。只需在调用.on()之前调用.off()即可。

这将删除所有事件处理程序:

$(element).off().on('click', function() {
    // function body
});

只删除已注册的'点击'事件处理程序:

$(element).off('click').on('click', function() {
    // function body
});

当一个事件被多次注册时,它将被多次触发(即使是同一个处理程序)。

如美元(“ctrl”)。On ('click', somefunction)如果每次页面部分刷新时都执行这段代码,则每次也会注册该事件。因此,即使ctrl只被单击一次,它也可能会执行“somefunction”多次——执行多少次取决于它被注册了多少次。

这对于任何用javascript注册的事件都是正确的。

解决方案:

确保只调用“on”一次。

出于某种原因,如果你不能控制架构,那么就这样做:

$ (" ctrl ") (.off’click’); $ (" ctrl ")。据(’click’somefunction);

在我的例子中,我用<script>标记在页面上加载了两次相同的*.js文件,所以两个文件都将事件处理程序附加到元素上。我删除了重复的声明,这就解决了问题。

所有关于.on()和.one()的东西都很棒,jquery也很棒。

但有时,你想让它更明显一点,用户不允许点击,在这种情况下,你可以这样做:

function funName(){
    $("#orderButton").prop("disabled", true);
    //  do a bunch of stuff
    // and now that you're all done
    setTimeout(function(){
        $("#orderButton").prop("disabled",false);
        $("#orderButton").blur();
    }, 3000);
}

你的按钮看起来是这样的:

<button onclick='funName()'>Click here</button>