0
function checkData() {
    var temp = 0;
    var totalMarks = countMark(temp);
    if (totalMarks != 100)
        window.alert("Marks must total 100");
}

function countMark(mark) {
    var totalMark = 0;
    totalMark += parseInt(mark)
    return totalMark;
}

function doAdd() {
    var taskid = document.getElementById("taskid").value;
    var taskname = document.getElementById("taskname").value;
    var taskmark = document.getElementById("taskmark").value;

    if (taskid.length === 0)
        window.alert("Task Id cannot be empty!");
    if (taskname.length === 0)
        window.alert("Task name cannot be empty!");
    if (taskmark.length === 0)
        window.alert("Task Mark cannot be empty!");
    else if (!markpattern.test(taskmark))
        window.alert("Invalid data in mark field");

    var marks = parseInt(document.getElementById("taskmark"));
    if (marks < 0 || marks > 100)
        window.alert("Marks out of range. Please re-enter");
    countMark(marks);
}

我的问题是当我继续调用 doAdd() 函数时。我的分数会不断增加。想要像在 C++ 中那样传递引用。我的函数 countMark(...) 将继续添加 .

之后,当我的表单提交时,如果我的总分不是 100,我的表单将调用函数 checkData() 。将提示警报和错误。

但我的代码不工作。我猜我的 countMark 函数在某个地方出错了

4

2 回答 2

0

If I understand you correctly, you're looking for the equivalent of a static variable - something that gets initialized the first time the function is called, and keeps it's value for subsequent calls.

Take a look at this related question: https://stackoverflow.com/a/1535650/2444111

The top answer (by CMS) is talking about class-based static variables, which are not quite the same thing.

The second answer (by Pascal MARTIN) is what you're looking for. It takes advantage of the fact that JS functions are also objects, and stores the variable as a property of the function object. This is a better solution than using a global variable (or a property of window, which is what a global actually is)

于 2013-06-15T08:40:49.067 回答
0

您的代码中有几个问题,很难说出您的意图是什么。但我会解决我发现的问题。

在下面的代码中,您请求一个 DOM 元素并尝试将其解析为整数。该类型转换的结果始终是NaN. 也许想像以前一样获取value元素的属性。(另外,不要多次请求同一个元素。请求一次,将结果保存在变量中,然后使用该变量)。

var marks = parseInt(document.getElementById("taskmark"));
if (marks < 0 || marks > 100)
    window.alert("Marks out of range. Please re-enter");
countMark(marks);

您的函数countMark非常没用,因为它总是会返回您传递给它的任何数字(请参阅代码中的注释)。

function countMark(mark) {
    var totalMark = 0; //create a new variable with value 0
    totalMark += parseInt(mark)  //add "mark" to that variable
    return totalMark;  //return that variable => 0 + mark = mark (and if mark = NaN => 0 + mark = NaN)
}

也许你想让 totalMark 成为一个全局变量,而不是你需要在你的函数之外定义它:

var totalMark = 0;
function countMark(mark) {
    totalMark += parseInt(mark);
    return totalMark;
}

最后但同样重要的是,让我们分析您的函数 checkData:

function checkData() {
    var temp = 0;  //create a local variable with value 0
    var totalMarks = countMark(temp);  //pass 0 to countMark => return 0 => totalMarks = 0
    if (totalMarks != 100) //always true since totalMarks is always 0
        window.alert("Marks must total 100"); //will always alert
}
于 2013-06-15T09:30:54.827 回答