Javascript If/Else问题

Javascript If/Else问题,javascript,Javascript,我想问的部分可能编码有点错误,但我会给出有效和无效的解决方案 第一个例子是: function SignUpClick(){ if (document.UserSignUp.UserEmail.value == document.UserSignUp.ConfirmEmail.value); else {alert ("Your email does not match - Please re-enter"); return;} if (document.UserSignUp.UserPassw

我想问的部分可能编码有点错误,但我会给出有效和无效的解决方案

第一个例子是:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value == document.UserSignUp.ConfirmEmail.value);
else {alert ("Your email does not match - Please re-enter"); return;}
if (document.UserSignUp.UserPassword.value == document.UserSignUp.ConfirmPassword.value);
else {alert ("Your password does not match - Please re-enter"); return;}}
第二个示例也适用于:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
else return true;}
但最后一个不起作用:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
else return true;
if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
{alert ("Your password does not match - Please re-enter"); return false;}
else return true;}
最后一个例子的有趣之处在于,第一个确认有效,但当我添加并测试第二个确认时,它就不起作用了,我也不完全确定原因。作为补充说明,我在没有第一个确认的情况下自己测试了第二个确认,它运行良好。与添加第二个或多个时有关


对于我可能缺少的内容有什么想法或建议吗?

在上一个示例中,它不会达到第二个
if
条件,因为您在第一个
if
else
条件中返回true

function SignUpClick()
{
   if (document.UserSignUp.UserEmail.value !=  document.UserSignUp.ConfirmEmail.value ) 
   {  
      alert ("Your email does not match - Please re-enter"); return false;
   }
   else 
      return true; // this line will ensure that second if will not be reached
   if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
   {
      alert ("Your password does not match - Please re-enter"); return false;
   }
   else 
     return true;
}
试试这个

function SignUpClick()
{
   if (document.UserSignUp.UserEmail.value !=  document.UserSignUp.ConfirmEmail.value ) 
   {  
      alert ("Your email does not match - Please re-enter"); 
      return false;
   }
   else if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
   {
      alert ("Your password does not match - Please re-enter"); 
      return false;
   }
   return true;
}

这是因为您在if-else情况下返回(第一个),因此第二个if-else不会被执行:)


这是因为当您执行
返回时,它会在此处结束函数,而不会继续到最后的if/else语句

您首先使用return-in-if-else来中断正在运行的代码, 试试这个-

function SignUpClick(){
    if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
    {
        alert ("Your email does not match - Please re-enter"); 
        return false;
    }
    else if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
    {
        alert ("Your password does not match - Please re-enter"); return false;
    }
    else return true;

}

在第一次if/else检查时,您只返回true或false。所以,当函数运行时,它将返回数据,并且永远不会进行密码检查。以下将起作用:

function SignUpClick(){
  if (document.UserSignUp.UserEmail.value 
    != document.UserSignUp.ConfirmEmail.value) {
    alert ("Your email does not match - Please re-enter"); return false;
  }
  if (document.UserSignUp.UserPassword.value 
    != document.UserSignUp.ConfirmPassword.value) {
    alert ("Your password does not match - Please re-enter"); return false;
  }
  return true;
}

我同意阿克谢的回答,因为他的回答更有意义。基本上他们说,因为我在第一个If和第一个Else中都有回报,所以它没有转到第二个If和Else。基本上,如果要有多个If&Else语句,那么每个If&Else只能有一个返回。我可以证实,因为我试过别人说的话,比如去掉假和真,我也试过让它看起来像
else{return True;}
else{return;}
。所以它与它是真是假以及它是否有{}无关

尽管有人说这只是因为第一个If and Else语句中的一个返回,或者因为在Else语句中有一个返回的If and Else语句。。。这是错误的,是一种假设。这是一个假设,因为我的第一个例子证明了这一点是错误的,因为它在Else和第二个If&Else中显示了回报

另外,当我测试AkshayJ所说的内容时,通过将代码更改为
else{}
或只是简单地删除else语句,这两种方法都让第二种方法起作用

另外,为了继续上面的两个段落/句子,返回符也可以在If语句中起作用,让它转到第二个If,因此它不仅仅在Else语句中起作用:

function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
{alert ("Your password does not match - Please re-enter"); return false;}}
上述代码有效。感谢阿克谢伊提供的信息,也感谢所有帮助过你的人

对于那些以Else-If为例的人来说,由于If&Else中的上下文,试图改变当前设置条件的动态流程是不好的做法。当他们已经分开的时候,我没有要求合并他们。如需参考,请访问此处:


所以,是的,联合收割机的例子是有效的,但这不是我所问的。它也不能解决这样一个事实,即使用这些组合示例,如果我添加另一个if&Else语句,它也不能解决问题。既然阿克谢伊解释了这一点,答案就归他们了,再次感谢。

@AkshayJ-它确实帮了不少忙,我不知道返回的原因。尽管现在我想我应该帮助他们两人以及其他可能在不知道Else If的情况下尝试使用Else If的人——这里的解释是:这就是我不接受他们答案的部分原因。=)
function SignUpClick(){
if (document.UserSignUp.UserEmail.value != document.UserSignUp.ConfirmEmail.value)
{alert ("Your email does not match - Please re-enter"); return false;}
if (document.UserSignUp.UserPassword.value != document.UserSignUp.ConfirmPassword.value)
{alert ("Your password does not match - Please re-enter"); return false;}}