首页 > 解决方案 > 确认无效

问题描述

我需要在我的数组中推送新对象。每个对象都包含属性(姓名、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;
    }

  }

标签: javascript

解决方案


您需要一个等号来进行赋值=

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);


推荐阅读