Bil*_*ljk 6 javascript if-statement prompt
所以,我有代码,没有完成,但我想要它做的只是显示一个警告框,如果我写"帮助",并说出其他任何其他输入.
function prompter() {
var reply = prompt("This script is made to help you learn about new bands, to view more info, type help, otherwise, just click OK")
if (reply === 'help' || 'Help')
{
alert("This script helps you find new bands. It was originally written in Python 3.0.1, using Komodo IDE, but was then manually translated into Javascript. Please answer the questions honestly. If you have no opinion on a question, merely press OK without typing anything.")
}
else
{
alert("Press OK to continue")
}
};
Run Code Online (Sandbox Code Playgroud)
但是,无论如何,即使您按下取消,第一个警报框也会弹出 我应该怎么解决这个???
And*_*ker 12
if (reply === 'help' || 'Help')
Run Code Online (Sandbox Code Playgroud)
应该:
if (reply === 'help' || reply === 'Help')
Run Code Online (Sandbox Code Playgroud)
既然'Help'是"真实的",所以if总是会输入第一部分.
当然,更好的做法是进行不区分大小写的比较:
if (reply.toLowerCase() === 'help')
Run Code Online (Sandbox Code Playgroud)
示例: http ://jsfiddle.net/qvEPe/