嵌套 if else 在 javascript 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15515697/
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
nested if else in javascript
提问by Manikandan Balasubramanian
I have trouble with the nested if structure in javascript. Any help is appreciated.
我在 javascript 中遇到嵌套 if 结构的问题。任何帮助表示赞赏。
function validateForm()
{
var a = document.forms["demo1"]["addr1"].value;
var b = document.forms["demo1"]["city"].value;
//var c = document.forms["demo1"]["fname"].value;
//var d = document.forms["demo1"]["lname"].value;
//var f = document.forms["demo1"]["phno"].value;
//var g = document.forms["demo1"]["email"].value;
//var g1 = document.forms["demo1"]["cemail"].value;
//var h = document.forms["demo1"]["pwd"].value;
//var h1 = document.forms["demo1"]["cpwd"].value;
if(b=="" || b==null)
{
alert("Please enter your city");
return false;
break;
}
else if(a=="" || a==null)
{
alert("Please enter your address");
return false;
break;
}
else {return true;}
}
<form name = "demo1" action = "exp2.php" onsubmit = "return validateForm()" method = "POST">
The code works fine(as intended) if there is only one if statement. But I am not getting the intended result if more than one if else is been deployed.
如果只有一个 if 语句,则代码工作正常(按预期)。但是,如果部署了多个 if else,我将无法获得预期的结果。
Regards, Mani
问候, 玛尼
回答by paxdiablo
First, you don't need break
statements, they're useless in this context.
首先,您不需要break
语句,它们在这种情况下是无用的。
Second, you don't need to nest and, in fact, you shouldn'tsince checking of a
and b
is independent of each other:
其次,您不需要嵌套,事实上,您不应该嵌套,因为检查a
和b
是相互独立的:
if(b=="" || b==null)
{
alert("Please enter your city");
return false;
}
if(a=="" || a==null)
{
alert("Please enter your address");
return false;
}
return true;
回答by rab
What about shorten ur code with reusable isEmpty
function
用可重用的isEmpty
功能缩短你的代码怎么样
function validateForm()
{
var isEmpty = function ( name , label ){
var val = document.forms["demo1"][ name ].value;
if( ! val )
{
alert("Please enter your "+ label );
return true;
}
return false;
}
return !isEmpty( 'city', 'city') &&
!isEmpty( 'addr1', 'address');
}
isEmpty return true
or false
isEmpty 返回true
或false
回答by Ronak Jain
if(b=="" || b==null) {
alert("Please enter your city");
return false;
}
else if(a=="" || a==null) {
alert("Please enter your address");
return false;
}
else {
return true;
}
回答by Xeli
Like paxdiablo says you can use two separate if statements.
就像 paxdiablo 说的,你可以使用两个单独的 if 语句。
But if you only want to require an address when a city is entered you must realize that this is not a nested if statement. This is:
但是如果您只想在输入城市时要求一个地址,您必须意识到这不是一个嵌套的 if 语句。这是:
if(b=="" || b==null)
{
alert("Please enter your city");
return false;
}
else
{
if(a=="" || a==null)
{
alert("Please enter your address");
return false;
}
else
{
return true;
}
}
A more reabable version would be, imo:
更可靠的版本是,imo:
if(b=="" || b==null)
{
alert("Please enter your city");
return false;
}
if((b=="" || b == null) && (a=="" || a==null))
{
alert("Please enter your address");
return false;
}