gpt4 book ai didi

javascript - 确认无效

转载 作者:行者123 更新时间:2023-11-29 16:33:33 24 4
gpt4 key购买 nike

我需要将新对象推送到数组中。每个对象都包含属性(姓名、sName、年龄、职业和显示所有用户信息的 show 方法)。数组正在由用户填充。 (迅速的)但我在确认时遇到问题。当我按“取消”时,它仍然继续工作。这是我的代码。

   var staff = [];


var askAgain = true;


while(askAgain==true) {


var employee = {
name: prompt("enter the name of the employee"),
sName: prompt("enter the sName of the employee"),
age: prompt("enter the age of the employee"),
occupation: prompt("enter the occupation of the employee"),
show: function(){

document.write(' employee: ' + staff[1].name + ' ' + staff[1].sName + ', ' + staff[1].age + ', ' + staff[1].occupation + ' <br> ' );} }


staff.push(employee);



console.log(staff);



window.confirm( "Would you like to go again?" );

if (confirm == true){
askAgain == true;}
else {
askAgain==false;
}

}

最佳答案

您需要一个等号来表示 assignment = .

if (window.confirm( "Would you like to go again?")) {
askAgain = true;
} else {
askAgain = false;
}

您可以只指定 window.confirm 的值.

askAgain = window.confirm( "Would you like to go again?");

当您开始收集至少一件元素时,您可以将 while 检查移至底部并直接使用确认,无需任何变量。

var staff = [],
employee;

do {
employee = {
name: prompt("enter the name of the employee"),
sName: prompt("enter the sName of the employee"),
age: prompt("enter the age of the employee"),
occupation: prompt("enter the occupation of the employee"),
};
staff.push(employee);
} while (window.confirm("Would you like to go again?"))

console.log(staff);

关于javascript - 确认无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com