jq点击事件多次响应_jQuery单击事件触发多次

我试图用Javascript编写视频扑克游戏,以降低其基础知识,但是我遇到了一个问题,其中jQuery click事件处理程序多次触发。

它们被附加到用于下注的按钮上,并且对于在游戏过程中第一手下注(仅触发一次)非常有效。但是在秒针下注中,每次按下一个下注或下注按钮都会触发两次点击事件(因此,每次按下正确的赌注量是两次)。总体而言,在按一次下注按钮时,触发单击事件的次数遵循此模式序列的第i个术语用于从游戏开始时下注第i个手:,7、11、16、22、29、37、46,无论值多少,它看起来都是n(n + 1)/ 2+1-而且我不够聪明,无法弄清楚,我使用OEIS。

这是正在起作用的click事件处理程序的函数;希望它很容易理解(让我知道是否可以,我也想做得更好):

/** 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

}

});

}

如果您有任何想法或建议,或者我的问题的解决方案是否类似于此处的另一个问题的解决方案,请让我知道(我看过许多标题相似的主题,也没有运气找到可行的解决方案为了我)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值