这个提示工作非常顺利,直到我更新了其他一些javascript。我不知道我是怎么搞砸的。此函数在body标记中声明为运行' onload'。
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
}
else if(answer == "no") {
alert('That is okay! See our links above and below to learn more!');
}
else if(answer == null || answer == "") {
alert('Please enter an answer.');
funcPrompt();
}
else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
现在我突然收到此错误并且提示不会出现。
答案 0 :(得分:2)
如果我们点击取消,提示将返回null
,并且无法在toLowerCase
上应用null
(会导致异常!)
在所有其他条件之前添加条件answer===null
,并return
停止执行function
function funcPrompt() {
var answer = prompt("Are you a photographer?", "Yes/No");
if (answer === null || answer === "") {
alert('Please enter an answer.');
funcPrompt();
return;
}
answer = answer.toLowerCase();
if (answer == "yes") {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else if (answer == "no") {
alert('That is okay! See our links above and below to learn more!');
} else {
alert('Sorry, that answer is not an option');
funcPrompt();
}
}
funcPrompt();

答案 1 :(得分:2)
不确定为什么会得到null,但是如果要避免使用null,请使用以下方法:
app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }))
答案 2 :(得分:0)
在您的情况下,最好使用确认而不是提示
function funcConfirm() {
var answer = confirm("Are you a photographer?");
if (answer === true) {
alert('Excellent! See our links above and below to see more work and find contact info!');
} else {
alert('That is okay! See our links above and below to learn more!');
}
}
funcConfirm();