循环中的Javascript变量名称

时间:2014-07-24 22:00:02

标签: javascript variables

我有一个函数可以创建6个html输入框来存储我的分数值,其中一个是总分。

var hole1 = prompt("Enter Hole 1 score:");
var hole2 = prompt("Enter Hole 2 score:");
var hole3 = prompt("Enter Hole 3 score:");
var hole4 = prompt("Enter Hole 4 score:");
var hole5 = prompt("Enter Hole 5 score:");

addScore()的全部功能:

function addScore() {

            var ni = document.getElementById('myDiv');
            var numi = document.getElementById('theValue');
            var num = (document.getElementById('theValue').value -1)+ 2;
            numi.value = num;

            var newdiv = document.createElement('div');
            var divIdName = 'my'+num+'Div';
            newdiv.setAttribute('id',divIdName);

            // Prompts user for their score
            var hole1 = prompt("Enter Hole 1 score:");
            var hole2 = prompt("Enter Hole 2 score:");
            var hole3 = prompt("Enter Hole 3 score:");
            var hole4 = prompt("Enter Hole 4 score:");
            var hole5 = prompt("Enter Hole 5 score:");

            // Make the boxes for index.html to be displayed
            var makeScoreBoxes ='<input type ="text" placeholder="Hole 1" id="hole1" /> <input type ="text" placeholder="Hole 2" id="hole2" /> <input type ="text" placeholder="Hole 3" id="hole3" /> <input type ="text" placeholder="Hole 4" id="hole4" /> <input type ="text" placeholder="Hole 5" id="hole5" /> <input type ="text" placeholder="Total Score" id="totalScore" />'
            newdiv.innerHTML = makeScoreBoxes;
            ni.appendChild(newdiv);

            // Adds up all scores entered via hole 
            totalScore = parseInt(parseFloat(hole1) +
                                                    parseFloat(hole2) +
                                                    parseFloat(hole3) +
                                                    parseFloat(hole4) +
                                                    parseFloat(hole5));

            // Places output of each data value from totalScore into input boxes in index.html
            addTotal = document.getElementById('totalScore').value=totalScore;
            addhole1 = document.getElementById('hole1').value=hole1; // takes prompt number entered in all hole* variables
            addhole2 = document.getElementById('hole2').value=hole2;
            addhole3 = document.getElementById('hole3').value=hole3;
            addhole4 = document.getElementById('hole4').value=hole4;
            addhole5 = document.getElementById('hole5').value=hole5;

            } // Adds up all scores entered via hole 
            totalScore = parseInt(parseFloat(hole1) +
                                    parseFloat(hole2) +
                                    parseFloat(hole3) +
                                    parseFloat(hole4) +
                                    parseFloat(hole5));

在index.html中调用它:

<center><p><a href="javascript:;" onclick="addScore();"><h1>Add score</h1></a></p>

如何为孔提示值(hole1,hole2,hole3,hole4等)和html输入框(hole1,hole2,hole3,)值生成唯一的变量名?

1 个答案:

答案 0 :(得分:0)

以下是可以对数组执行的操作的示例:

var holes = [];

for (var i = 1; i <= 5; i++) {
    holes.push(prompt("Enter Hole " + i + " score:"));
}

var totalScore = 0;

for (var i = 0; i < holes.length; i++) {
    totalScore += parseInt(holes[i], 10);
}

alert("total score: " + totalScore);

演示:http://jsfiddle.net/EhpPZ/