if javascript中的语句总是如此

前端之家收集整理的这篇文章主要介绍了if javascript中的语句总是如此前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以,我有代码,没有完成,但我想要它做的只是显示一个警告框,如果我写“帮助”,并说出其他任何其他输入.
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')

由于’帮助’是“真实的”,因此将始终输入if的第一部分.

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

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

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

原文链接:https://www.f2er.com/js/156134.html

猜你在找的JavaScript相关文章