根据单击的按钮在javascript中保存全局变量

根据单击的按钮在javascript中保存全局变量,javascript,jquery,function,variables,click,Javascript,Jquery,Function,Variables,Click,我正在尝试将基于console.log()/prompt的javascript游戏切换为可供浏览器使用的可视化游戏。为此,我将jQuery与javascript混合使用 1) 我试图根据用户单击的按钮设置nHands=1、2或3。我有3个按钮,它们具有相同的.smallbutton类和唯一的.oneh.twoh和.3h类 2) 用户单击3个按钮中的任何一个后,所有按钮都将消失,使用$('.smallbutton').detach()。这个很好用 问题出在上面。它似乎没有将nHands=设置为任何

我正在尝试将基于
console.log()/prompt
的javascript游戏切换为可供浏览器使用的可视化游戏。为此,我将jQuery与javascript混合使用

1) 我试图根据用户单击的按钮设置
nHands=1、2或3
。我有3个按钮,它们具有相同的
.smallbutton
类和唯一的
.oneh
.twoh
.3h

2) 用户单击3个按钮中的任何一个后,所有按钮都将消失,使用
$('.smallbutton').detach()。这个很好用

问题出在上面。它似乎没有将
nHands=
设置为任何值。因为为了我的
recordBetAmount()要运行/执行某项操作,需要将其设置为1、2或3。为什么nHands不等于任何值

这是小提琴。请注意,小提琴包括我尝试的第二种方式

以下是我代码的相关部分:

var hand1, hand2, hand3, hD, nHands; //global vars

function numHands() {
    $('<p>Choose the number of hands you want to play.</p>').appendTo('.message');
    $('.oneh').click(function () {
        nHands = 1;
        $('.smallbutton').detach();
    });
    $('.twoh').click(function () {
        nHands = 2;
        $('.smallbutton').detach();
    });
    $('.threeh').click(function () {
        nHands = 3;
        $('.smallbutton').detach();
    });
    if (nHands > 0 && nHands < 4) {
        x = 150000 / nHands;
        if (nHands > 0) {
            hand1 = new Hand("First Hand", x, x, ".hand1");
            if (nHands > 1) {
                hand2 = new Hand("Second Hand", x, x, ".hand2");
                if (nHands > 2) {
                    hand3 = new Hand("Third Hand", x, x, ".hand3");
                }
            }
        }
    } else {
        $('<p>ERROR!!!</p>').appendTo('.message');
    }
}

....
numHands();
recordBetAmount();
var hand1、hand2、hand3、hD、nHands//全局变量
函数numHands(){
$(“选择您想玩的牌数。

”).appendTo(“.message”); $('.oneh')。单击(函数(){ nHands=1; $('.smallbutton').detach(); }); $('.twoh')。单击(函数(){ nHands=2; $('.smallbutton').detach(); }); $('.threeh')。单击(函数(){ nHands=3; $('.smallbutton').detach(); }); 如果(nHands>0&&nHands<4){ x=150000/nHands; 如果(nHands>0){ 手1=新手(“第一手”,x,x,“.hand1”); 如果(nHands>1){ 手2=新手(“二手”,x,x,“.hand2”); 如果(nHands>2){ 手3=新手(“第三手”,x,x,“.hand3”); } } } }否则{ $(“错误!!!

”).appendTo(“.message”); } } .... numHands(); recordBetAmount();

编辑:我甚至尝试将上面的numHands函数分成两个单独的函数,但似乎仍然没有将nHands=设置为任何值。

尝试将numHands函数逻辑分成两个函数。首先将事件处理程序附加到按钮上,另一个将按照下面的代码进行计算。并将您的代码封装在$(document).ready();。根据JSFIDLE示例,您正在将nHands参数传递给函数,以便删除全局nHands diclariation。是JSFIDLE,它可以工作。另一件事是,如果读取全局变量nhand,则其他函数(如recordBetAmount等)应该相应地更改。因为它们不识别声明的nHands变量。它给出了
uncaughtreferenceerror:nHands未在控制台输出中定义。应修改recordBetAmount函数。当文档准备就绪时调用它。然而,在我看来,这个函数应该在人们下注后调用

$(document).ready(function()
{    
suits = ["s", "d", "c", "h"];
deck = [];
var hand1, hand2, hand3, hD; //global vars

   ...

function numHands() {
    var nHands;
    $('<p>Choose the number of hands you want to play.</p>').appendTo('.message');
        $('.oneh').click(function() {
            nHands = 1;
            $('.smallbutton').detach();
            numhands2(nHands); });
        $('.twoh').click(function() {
            nHands = 2;
            $('.smallbutton').detach();
            numhands2(nHands); });
        $('.threeh').click(function() {
            nHands = 3;
            $('.smallbutton').detach();
            numhands2(nHands); });

function numhands2(nHands) {
    console.log(nHands);
    if (nHands > 0 && nHands < 4) {
        x = 150000/nHands;
        if (nHands > 0) { hand1 = new Hand("First Hand", x, x, ".hand1");
            if (nHands > 1) { hand2 = new Hand("Second Hand", x, x, ".hand2");
                if (nHands > 2) { hand3 = new Hand("Third Hand", x, x, ".hand3"); } }
        hD = new Hand("Dealer", 4500200 , 4500200 , ".handD"); } }
    else { $('<p>ERRRORRRRR</p>').appendTo('.message'); } }
}

....

});
$(文档).ready(函数()
{    
诉讼=[“s”、“d”、“c”、“h”];
甲板=[];
变量hand1,hand2,hand3,hD;//全局变量
...
函数numHands(){
var nHands;
$(“选择您想玩的牌数。

”).appendTo(“.message”); $('.oneh')。单击(函数(){ nHands=1; $('.smallbutton').detach(); numhands2(nHands);}; $('.twoh')。单击(函数(){ nHands=2; $('.smallbutton').detach(); numhands2(nHands);}; $('.threeh')。单击(函数(){ nHands=3; $('.smallbutton').detach(); numhands2(nHands);}; 函数numhands2(nHands){ console.log(nHands); 如果(nHands>0&&nHands<4){ x=150000/nHands; 如果(nHands>0){hand1=新手(“第一手”,x,x,“.hand1”); 如果(nHands>1){hand2=新手(“二手”,x,x,“.hand2”); 如果(nHands>2){hand3=新手(“第三手”,x,x,“.hand3”);} hD=新手(“经销商”,4500200,4500200,“.handD”);} else{$('errorrrr

).appendTo('.message');} } .... });
或marge numHands2和recordBetAmount函数

function numhands2(nHands) {
    console.log(nHands);
    if (nHands > 0 && nHands < 4) {
        x = 150000/nHands;
        if (nHands > 0) { hand1 = new Hand("First Hand", x, x, ".hand1");
            if (nHands > 1) { hand2 = new Hand("Second Hand", x, x, ".hand2");
                if (nHands > 2) { hand3 = new Hand("Third Hand", x, x, ".hand3"); } }
        hD = new Hand("Dealer", 4500200 , 4500200 , ".handD"); } }
    else { $('<p>ERRRORRRRR</p>').appendTo('.message'); } }
}

function recordBetAmount() {
    if (nHands > 0) { setBetAmount(hand1);
        if (nHands > 1) { setBetAmount(hand2);
            if (nHands > 2) { setBetAmount(hand3); } } } }
函数numhands2(nHands){
console.log(nHands);
如果(nHands>0&&nHands<4){
x=150000/nHands;
如果(nHands>0){hand1=新手(“第一手”,x,x,“.hand1”);
如果(nHands>1){hand2=新手(“二手”,x,x,“.hand2”);
如果(nHands>2){hand3=新手(“第三手”,x,x,“.hand3”);}
hD=新手(“经销商”,4500200,4500200,“.handD”);}
else{$('errorrrr

).appendTo('.message');} } 函数recordBetAmount(){ 如果(nHands>0){setBetAmount(hand1); 如果(nHands>1){setBetAmount(hand2); 如果(nHands>2){setBetAmount(hand3);}}}
我试着做你想做的,只是做了一些改变:

$(document).ready(function () {
    var Hand = function Hand() {
        //your Hand class
    };

    var NumHands = function NumHands(callback) {
        var $this = this;
        this.callback = callback;
        this.hand1 = null;
        this.hand2 = null;
        this.hand3 = null;
        this.hD = null;
        this.nHands = null;

        $('<p>Choose the number of hands you want to play.</p>').appendTo('.message');
        $('.oneh').click(function () {
            $this.detachAndCreate(1);
        });
        $('.twoh').click(function () {
            $this.detachAndCreate(2);
        });
        $('.threeh').click(function () {
            $this.detachAndCreate(3);
        });
        this.detachAndCreate = function (val) {
            this.nHands = val;
            $('.smallbutton').detach();
            this.createHands();
            if (jQuery.isFunction(this.callback)) this.callback.call(this);
        };
        this.createHands = function () {
            if (this.nHands > 0 && this.nHands < 4) {
                var x = 150000 / this.nHands;
                if (this.nHands > 0) {
                    this.hand1 = new Hand("First Hand", x, x, ".hand1");
                    if (this.nHands > 1) {
                        this.hand2 = new Hand("Second Hand", x, x, ".hand2");
                        if (this.nHands > 2) {
                            this.hand3 = new Hand("Third Hand", x, x, ".hand3");
                        }
                    }
                }
            } else {
                $('<p>ERROR!!!</p>').appendTo('.message');
            }
        };
    };

    function recordBetAmount () {
        //here 'this' refers to numHands instance
    };

    var numHands = new NumHands(recordBetAmount);
});
$(文档).ready(函数(){
var Hand=函数Hand(){
//你的手工课
};
var NumHands=函数NumHands(回调){
var$this=这个;
this.callback=回调;
this.hand1=null;
this.hand2=null;
this.hand3=null;
this.hD=null;
this.nHands=null;
$(“选择您想玩的牌数。

”).appendTo(“.message”); $('.oneh')。单击(函数(){ $this.detachAndCreate(1); }); $('.twoh')。单击(函数(){ $this.detachAndCreate(2); }); $('.threeh')。单击(函数(){ $this.detachAndCreate(3); }); this.detacheandcreate=函数(val){ this.nHands=val; $('.smallbutton').detach(); 这是createHands(); if(jQuery.isFunction(this.callback))this.callback.call(this); }; this.createHands=函数(){ 如果(这个)
var hand1, hand2, hand3, hD, nHands; //global vars

function recordBetAmount() {
  alert("Finally nHands is " + nHands);
}

$(document).ready(function () {
       $('.oneh').click(function() {
            nHands = 1;
            alert("First " + nHands);
            $('.smallbutton').detach(); 
            numHands();
        });
        $('.twoh').click(function() {
            nHands = 2;
            alert("Second " + nHands);
            $('.smallbutton').detach(); 
            numHands();
        });
        $('.threeh').click(function() {
            nHands = 3;
            alert("Third " + nHands);
            $('.smallbutton').detach(); 
            numHands();
        });
});

function numHands() {
    $('<p>Choose the number of hands you want to play.</p>').appendTo('.message');

    recordBetAmount();
    if (nHands > 0 && nHands < 4) {
        x = 150000/nHands;
        if (nHands > 0) { hand1 = new Hand("First Hand", x, x, ".hand1");
            if (nHands > 1) { hand2 = new Hand("Second Hand", x, x, ".hand2");
                if (nHands > 2) { hand3 = new Hand("Third Hand", x, x, ".hand3");
        } } } }
    else { 
        $('<p>ERROR!!!</p>').appendTo('.message'); 
    }


}