0

我需要在循环中调用一个函数。

我有以下代码....

do {
    var name = prompt("Enter name:");

    if (!isNaN(age) && name != null && name != "") {
        names[i] = name;
    }
    loop = confirm("Add new name?");

    i++;
    // at this place I want to call the function
    // addnew(document.getElementById("newtable")"; so when someone clicks cancel in the confirm box the javascript creates a dynamic table from the array names
} while (loop);​

任何人都知道我如何调用函数addnew?

4

2 回答 2

0

我猜您希望在 Confirm 回答是时调用该函数,并在没有时终止循环,这可以像这样实现:

while(true) {
    var name = prompt("Enter name:");
    if (!name) {
        break;
    }

    if (!isNaN(age)) {
        names[i] = name;

    }


    if (!confirm("Add new name?")) {
        break;
    }

    i++;
    // at this place I want to call the function
    addnew(document.getElementById("newtable")); 
}
于 2012-10-13T21:45:20.903 回答
0

你是否想做这样的事情:

var name,
names = [];

function coolFunc(names) {
    console.log(names);
}

do {
    var name = prompt("Enter name:");

    if (name != null && name != "") {
        names.push(name);
    }
    loop = confirm("Add new name?");

//  if you want to handle the names one-by-one as you get them, then you could call 
//  your function here, otherwise call it when you exit the loop as below

} while (loop);

coolFunc(names);

我删除了测试,age因为你发布的内容中没有任何内容表明它来自哪里,所以抛出了一个错误,所以你需要在适当的时候重新处理它,而且i看起来并没有必要,但是也抛出错误。

于 2012-10-13T22:05:38.293 回答