简体   繁体   English

在 Javascript IF-ELSE 中,IF 语句不起作用

[英]In Javascript IF-ELSE, IF Statement is not working

I am trying to work on verifying OTP.我正在尝试验证 OTP。 Here I have two components that are:在这里,我有两个组件:

  1. Textbox which takes input of OTP.接受 OTP 输入的文本框。 id="txtOTP" id="txtOTP"
  2. An Status Line (here i have used <i> tag) that shows status of verified OTP.显示已验证 OTP 状态的状态行(这里我使用了<i>标记)。 id="statusLine" id="状态行"

I am using JavaScript for this purpose.为此,我正在使用 JavaScript。

function checkOTP() 
{
  var OTP = "1234";
  var txtOTP = document.getElementById('txtOTP');
  var statusLine = document.getElementById('statusLine');
  var myOTP = txtOTP.value;

  if (OTP.value == myOTP) 
 {
    console.log('Entered in Valid OTP');
    statusLine.style.display = "inline";
    statusLine.style.color = "green";
    statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
    console.log('Exit From Valid OTP');
    return true;
  } 
  else if (OTP.value != myOTP) 
  {
    console.log('Entered in Invalid OTP');
    statusLine.style.display = "inline";
    statusLine.style.color = "red";
    statusLine.innerHTML = "Invalid OTP. Please Try Again";
    console.log('Exit From Invalid OTP');
    return false;
  }
}

As Per my code it should go to the if's scope if OTP is correct, and it should go to the else's scope if OTP is wrong.根据我的代码,如果 OTP 正确,它应该进入 if 的范围,如果 OTP 错误,它应该进入 else 的范围。 However, it always goes to the else's scope even though I am writing the correct OTP in the textbox.但是,即使我在文本框中编写了正确的 OTP,它也总是进入 else 的范围。 I have even tried this code without using if with the else statement (like else if() { } ).我什至尝试过这段代码,而没有将 if 与 else 语句一起使用(如 else if() { } )。

You need to either change myOTP to a number or use double equals:您需要将myOTP更改为数字或使用双等号:

var myOTP = parseInt(txtOTP.value);

Or:或者:

if (OTP == myOTP) {...}

Also note that you don't need else if (...) - just use else {...} .另请注意,您不需要else if (...) - 只需使用else {...}

OTP is a Number but you check OTP.value in if/else if statements OTP是一个Number但您在 if/else if 语句中检查OTP.value

function checkOTP()
            {
                var OTP = 1234;
                var txtOTP = document.getElementById('txtOTP');
                var statusLine = document.getElementById('statusLine');
                var myOTP = txtOTP.value;

                if(OTP === myOTP )
                {
                    console.log('Entered in Valid OTP');
                    statusLine.style.display = "inline";
                    statusLine.style.color = "green";
                    statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
                    console.log('Exit From Valid OTP');
                    return true;
                }
                else if(OTP != myOTP )
                {
                    console.log('Entered in Invalid OTP');
                    statusLine.style.display = "inline";
                    statusLine.style.color = "red";
                    statusLine.innerHTML = "Invalid OTP. Please Try Again";
                    console.log('Exit From Invalid OTP');
                    return false;
                }
            }

Here is a solution.这是一个解决方案。 Its based on the comments and previous answers:它基于评论和以前的答案:

function checkOTP() {
    var OTP = "1234";
    var txtOTP = document.getElementById('txtOTP');
    var statusLine = document.getElementById('statusLine');
    var myOTP = txtOTP.value;

    if (OTP == myOTP) {
        console.log('Entered in Valid OTP');
        statusLine.style.display = "inline";
        statusLine.style.color = "green";
        statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
        console.log('Exit From Valid OTP');
        return true;
    }  else {
        console.log('Entered in Invalid OTP');
        statusLine.style.display = "inline";
        statusLine.style.color = "red";
        statusLine.innerHTML = "Invalid OTP. Please Try Again";
        console.log('Exit From Invalid OTP');
        return false;
    }
}

You needed to write OTP instead of OTP.value and you don't need and else if for the opposite.您需要编写OTP而不是OTP.value并且您不需要, else if相反。 Just else will do.只是else会做。

try adding a else statement after the else if since the syntax is :尝试在 else if 之后添加一个 else 语句,因为语法是:

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM