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 に答える