Javascript 否则如果javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12291092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:38:05  来源:igfitidea点击:

else if javascript

javascriptif-statement

提问by Tracie Richardson

I have a simple function that is supposed to prompt the user for the company name, followed by a confirmation message box that asks the user "Would you like to order a Web Design package today?" The problem is with my 'if' and 'else if' statements. The 'if' statement should show an alert box "Thank you for your order" when the user clicks 'OK'; and an 'else if' statement that should show an alert box "We appreciate your time" when the user clicks 'Cancel'. The function is called by an onClick script. The prompt and confirmation boxes appear but I can't figure out why my if and else if boxes do not appear. What am I missing? Any help is appreciated. Thanks.

我有一个简单的功能,应该提示用户输入公司名称,然后是一个确认消息框,询问用户“您今天要订购网页设计包吗?” 问题在于我的“if”和“else if”语句。当用户单击“确定”时,“if”语句应显示一个警告框“感谢您的订单”;以及一个“else if”语句,当用户单击“取消”时,该语句应显示一个警告框“我们感谢您的时间”。该函数由 onClick 脚本调用。出现提示和确认框,但我不知道为什么我的 if 和 else if 框没有出现。我错过了什么?任何帮助表示赞赏。谢谢。

<SCRIPT language = "Javascript">

function submitOrder() {

var companyName = prompt("Please enter company name.", "")

var willOrderPackage = confirm("Would you like to order a Web Design package today?")

if (willOrderPackage == "true") {
   alert("Thank you for your order.")

}else if{
   alert("We appreciate your time.")
}
</SCRIPT>

回答by jamesinc

There are actually two different if..else constructs. The first is a simple if-else:

实际上有两种不同的 if..else 结构。第一个是一个简单的 if-else:

if ( condition ) {
   // Do stuff if condition is true
} else {
   // Do stuff if condition is not true
}

The next is an if-else-if:

接下来是一个if-else-if:

if ( condition ) {
   // Do stuff if condition is true
} else if ( differentCondition ) {
   // Do stuff if differentCondition is true
} else {
   // Do stuff if neither condition nor differentCondition is true
}

You can also use else-if as many times as you like, e.g.:

您还可以根据需要多次使用 else-if,例如:

if ( condition ) {
   // Do stuff
} else if ( condition2 ) {
   // etc
} else if ( condition3 ) {
   // etc
} else if ( condition4 ) {
   // etc
} else {
   // etc
}

And every part of an if..else is optional, except for the initial if block. So if you don't want to do anything if conditionis not true, you can just omit the else block entirely:

并且 if..else 的每个部分都是可选的,除了初始的 if 块。所以如果你不想做任何 ifcondition不是真的,你可以完全省略 else 块:

if ( condition ) {
   // do stuff if condition is true
}

HTH

HTH

Going beyond your question for a moment, the expression being evaluated in your if statement's condition is a bit wibbly-wobbly.

暂时超越您的问题,在您的 if 语句的条件中评估的表达式有点摇摆不定。

willOrderPackage will always be trueor false, but it's important to note that trueand "true"or falseand "false"are different. The first is boolean, the second is a string.

willOrderPackage 将始终为trueor false,但重要的是要注意trueand"true"falseand"false"是不同的。第一个是布尔值,第二个是字符串。

So, your if statement should be asking:

所以,你的 if 语句应该问:

if ( willOrderPackage == true ) {

Even better than that, when you evaluate an expression in an if statement, there's an implied == trueat the end of it which is invisible. For instance:

比这更好的是,当您在 if 语句中评估表达式== true时,它的末尾有一个不可见的隐含。例如:

if ( willOrderPackage == true ) {

would be interpreted as :

将被解释为:

if ( (willOrderPackage == true) == true )

The benefit of this is that you can actually omit the whole == truebit from your code, so you can just write:

这样做的好处是您实际上可以== true从代码中省略所有部分,因此您可以编写:

if ( willOrderPackage ) {

And effectively you're still saying "if willOrderPackage is true"

并且实际上你仍然在说“如果 willOrderPackage 是真的”

Hope that helps clear up a few things for you!

希望这有助于为您澄清一些事情!

回答by mrmryb

your else ifhas no condition, use else

else if没有条件,使用else

as stated

就像声明的那样

if (willOrderPackage == "true") {
alert("Thank you for your order.")

is wrong: willOrderPackage is a boolean so it can be

是错误的:willOrderPackage 是一个布尔值,所以它可以是

(willOrderPackage == true)

or even better

甚至更好

(willOrderPackage)

回答by mgraph

also it's better to use :

最好使用:

if (willOrderPackage) {

instead of :

代替 :

if (willOrderPackage == "true") {

回答by Finding Nemo 2 is happening.

Change to:

改成:

if (willOrderPackage) {
   alert("Thank you for your order.")

}else{
   alert("We appreciate your time.")
}

回答by Christian

This is how it's properly done:

这是它的正确完成方式:

<script type="text/javascript">

function submitOrder() {
    var companyName = prompt("Please enter company name.", "");
    var willOrderPackage = confirm("Would you like to order a Web Design package today?");

    if (willOrderPackage) {
        alert("Thank you for your order.");
    }else{
        alert("We appreciate your time.");
    }
}

</script>

Things I did:

我做的事情:

  • modernized coding style
  • switched to type attribute instead of language
  • added semicolons on each ending line
  • removed redundant ifkeyword
  • added a closing curly bracket which was originally missing
  • removed comparison with a string true, comparison with boolean values do not need this
  • 现代化的编码风格
  • 切换到类型属性而不是语言
  • 在每个结束行上添加分号
  • 删除多余的if关键字
  • 添加了一个最初丢失的结束大括号
  • 删除了与字符串的比较 true,与布尔值的比较不需要这个