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

时间:2019-04-22 12:35:26

标签: javascript html css

我正在尝试验证OTP。这里有两个组件:

  1. 文本框,用于输入OTP。 id =“ txtOTP”
  2. 状态行(这里我使用了<i>标签),显示了已验证OTP的状态。 id =“ statusLine”

我正在为此目的使用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;
  }
}

按照我的代码,如果OTP正确,它应该进入if的范围,如果OTP错误,它应该进入else的范围。 但是,即使我在文本框中编写了正确的OTP,也始终会进入其他范围。我什至试过了这段代码,没有在else语句中使用if(例如else if(){})。

4 个答案:

答案 0 :(得分:2)

您需要将myOTP更改为数字或使用双倍等于:

var myOTP = parseInt(txtOTP.value);

或者:

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

还请注意,您不需要else if (...)-只需使用else {...}

答案 1 :(得分:0)

OTPNumber,但您在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;
                }
            }

答案 2 :(得分:0)

这是一个解决方案。其基于评论和先前的答案:

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

您需要写OTP而不是OTP.value,而您不需要写else if。只需else即可。

答案 3 :(得分:-2)

如果语法为,请尝试在else之后添加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
}