javascript - if 语句在 javascript 中始终为真

标签 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")
  }
};

但是,无论如何,第一个警告框弹出,即使你按下取消! 我该如何解决这个问题???

最佳答案

if (reply === 'help' || 'Help')

应该是:

if (reply === 'help' || reply === 'Help')

因为 'Help' 是“真实的”,所以 if 的第一部分将始终被输入。

当然,更好的做法是进行不区分大小写的比较:

if (reply.toLowerCase() === 'help')

示例: http://jsfiddle.net/qvEPe/

关于javascript - if 语句在 javascript 中始终为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9303160/

相关文章:

java - 如何使用断言命令?

r - 在 R 中使用 exists 语句

prompt - 在 Fish Shell 中获取当前目录(没有完整路径)

c - 伴随其他值时的 EOF 行为

javascript - 安装 meteor 帐户输入包时出错

javascript - Node.js 异步 forEach : thread-safe?

javascript - 从我的 jQuery 插件中移除延迟

javascript - 如何打印指定 MySQL 表的递归、有序、依赖表的列表

php - 使用我在查询中选择的内容在 php 中创建 if 方法?

javascript - 为什么在浏览器中打开网页时功能不执行?