简体   繁体   English

.on('click', function(){}) 仅适用于第一次点击

[英].on('click', function(){}) only working on first click

I'm making a Blackjack game to exercise my Javascript skills.我正在制作二十一点游戏来锻炼我的 Javascript 技能。 I had a bunch of alert() messages tied to the betting function to prevent invalid entries.我有一堆与投注 function 相关的alert()消息,以防止无效条目。 In updating the code to have a more elegant message style than a browser alert, I wrote a function called alertModal() that pops up a message on the screen and then fades away.在更新代码以获得比浏览器警报更优雅的消息样式时,我编写了一个名为alertModal()的 function,它会在屏幕上弹出一条消息,然后消失。 The message pops up the first time a user tries to enter an invalid bet, but does not pop up any other messages if the bet is invalid-- nothing happens.当用户第一次尝试输入无效投注时,该消息会弹出,但如果投注无效,则不会弹出任何其他消息——没有任何反应。 I know the placeBet() function is still running when the user clicks again, because if the bet is valid, dealFirstCards() runs and the game proceeds.我知道当用户再次单击时placeBet() function 仍在运行,因为如果投注有效, dealFirstCards()运行并且游戏继续。 So it seems to me that for some reason, the if/else portion of the placeBet() function is only running on the first click...所以在我看来,由于某种原因, placeBet() function 的 if/else 部分仅在第一次单击时运行...

The game is live and running with this code at http://cnb-blackjack.netlify.com/game.html游戏在http://cnb-blackjack.netlify.com/game.html使用此代码实时运行

Here is the javascript code in question:这是有问题的 javascript 代码:


// Player places a bet
    $('div.bet').on('click', function() {
        $(this).removeClass('glow');
        $('.bet-button').addClass('glow');
    });
    $('.bet-button').on('click', function() {
        event.preventDefault();
        if (!placeBet.called) {
            placeBet();
        }
    });

// PLACE BET
    function placeBet() {
        var $bet = parseInt($('.bet-input').val())
        var $bank = parseInt($('.player-bank').text())
        var $currentBet = $('.current-bet');
        if (!isNaN($bet) && $bet <= $bank && $bet !== 0) {
            $currentBet.text(' $' + $bet);
            $('.bet input[type="text"]').val('');
            $('.place-bet .hideaway').slideUp();
            $('.player-bank').text($bank - $bet);
            placeBet.called = true;
            dealFirstCards();
        } else if ($bet > $bank) {
            var $message = 'Bet cannot exceed the amount in your Bank!';
            alertModal($message);
        } else if (isNaN($bet)) {
            var $message = 'Enter a number, without "$".';
            alertModal($message);
        } else if ($bet === 0) {
            var $message = "Betting nothing won't get you very far...";
            alertModal($message);
        }
    }

// SHOW MODAL
    function alertModal(message) {
        $popUp = $('.alert-message');
        $('.modal').removeClass('hide');
        $popUp.text(message);
        setTimeout(function() {
            $('.modal').fadeOut(1000);
        }, 1000);
    }
function alertModal(message) {

        $popUp = $('.alert-message');
        $('.modal').show();
        $popUp.text(message);
        setTimeout(function() {
            $('.modal').fadeOut(1000);
        }, 1000);
    }

As comments have explained, fadeOut is leaving the modal hidden after the first time it's clicked.正如评论所解释的,fadeOut 在第一次单击后将模式隐藏起来。 Just call $(element).show();只需调用 $(element).show(); on the modal to show it again and let fadeOut remove it.在模态框上再次显示它并让 fadeOut 将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM