否则如果javascript [英] else if javascript

查看:172
本文介绍了否则如果javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

<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>


推荐答案

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

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

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

下一个是if-else-if:

The next is an 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
}

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

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

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

if..else的每个部分都是可选的,除了最初的if块。因此,如果您不想做任何事情,如果条件不成立,您可以完全省略else块:

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 condition is not true, you can just omit the else block entirely:

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

HTH

暂时超出您的问题,在if语句中评估的表达式有点粗糙-wobbly。

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

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

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

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

So, your if statement should be asking:

if ( willOrderPackage == true ) {

甚至比那更好,当你在if语句中计算一个表达式时,在它的末尾有一个隐含的 == true ,它是不可见的。例如:

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

if ( willOrderPackage == true ) {

将被解释为:

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

这样做的好处是你实际上可以省略整个 == true 来自你的代码,所以你可以写:

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

if ( willOrderPackage ) {

实际上你还在说如果willOrderPackage是真的

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

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

Hope that helps clear up a few things for you!

这篇关于否则如果javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆