Javascript函数改变全局变量

时间:2017-09-06 11:59:23

标签: javascript json

所以我正在制作一个评分系统,在html左侧和右侧都有输入框,它会将它们加起来决定哪一方是赢家。决定胜负后,我希望它为获胜的团队的全局变量“得分”添加一个点。将会有另一个html页面显示哪个团队处于领先地位。这些是我在开始时对全局变量的设置。

var Team1 = {"Name":"Rockford Hills",
             "score": 0}
var Team2 = {"Name":"Pacific Bluffs",
             "score": 0}
var Team3 = {"Name":"Downtown",
             "score": 0}
var Team4 = {"Name":"Vinewood",
             "score": 0}
var Team5 = {"Name":"La Puerta",
             "score": 0}
var Team6 = {"Name":"Chumash",
             "score": 0}
var Team7 = {"Name":"Vespucci",
             "score": 0}
var Team8 = {"Name":"Los Santos",
             "score": 0}

这是将分数添加到分数对象的代码,唯一的问题是当我进入另一个链接到同一个java脚本表的html页面时,看看它们都在0上的分数,因为这个函数没有不要覆盖全局变量。我只是需要一些帮助来理解这一切是如何工作的,我哪里出错了,或者我是否可以解决它,因为我有点像业余爱好者。

function submitR1M1(){
    var rightScores = document.getElementsByName('team2');
    var totalRight=0;
    for(var i=0;i<rightScores.length;i++){
        if(parseInt(rightScores[i].value))
            totalRight += parseInt(rightScores[i].value);
    }
    var leftScores = document.getElementsByName('team1');
    var totalLeft=0;
    for(var i=0;i<leftScores.length;i++){
        if(parseInt(leftScores[i].value))
            totalLeft += parseInt(leftScores[i].value);
    }

    if (totalLeft > totalRight)
        Team1.score = Team1.score + 1;
    else
        (Team2.score = Team2.score + 1);
    console.log(Team1.Name + Team1.score + Team2.Name + Team2.score);

}

0 个答案:

没有答案