javascript - 确认无效
问题描述
我需要在我的数组中推送新对象。每个对象都包含属性(姓名、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;
}
}
解决方案
您需要一个等号来进行赋值=
。
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);
推荐阅读
- angular - 在将 html 转换为 pdf 时,当 PDF 到达第二页时,文本会中断。请帮帮我?
- c++ - 为什么这个 MSVC asm 块没有 ret,或者非 void 函数有返回?
- elasticsearch - 如何使用 Trino/Presto 连接 Elasticsearch?
- swift - 在 main.storyboard 中,圆形按钮未加载
- php - Woocommerce 从产品前端和管理面板中排除类别
- php - 自定义字段上的 WordPress 自定义搜索
- django - Django - 更新用户帐户会破坏用户密码
- javascript - Vue 3 通过组件到达使用错误的功能
- python - Python(Numpy)无法重命名列 - 它们变成“NaN”
- javascript - css 背景图像在不同图像大小的过渡时拉伸