简体   繁体   English

如果函数中的语句不起作用

[英]If statements in functions not working

For some unknown reason the if statements in the checkID function have stopped working and are only displaying 'OK' no matter what input is entered. 由于某些未知原因,checkID函数中的if语句已停止工作,无论输入什么输入,它们都只显示“OK”。 I'm not exactly sure what I've done wrong but can someone take a look at my code. 我不确定我做错了什么,但有人可以看看我的代码。 At the moment the code only seems to be running the last if statement in the function and only the last message which is "Ok". 目前,代码似乎只运行函数中的最后一个if语句,而只是最后一条消息“Ok”。

This is my HTML: 这是我的HTML:

 function begin() { var Name = document.forms["Cars"]["Name"].value; var NameID = document.forms["Cars"]["NameID"].value; var IDName = ""; var PersonID = ""; var checklength = Name.length; CheckName(); console.log(checklength, checklength < 100) var checkIDlength = NameID.length; checkID(); console.log(checkIDlength, checkIDlength < 100) return false; function CheckName() { if (checklength > 0 && checklength < 21) { IDName = "Ok" } else { IDName = "Invalid Name Length"; } document.getElementById('IDName').innerHTML = IDName; return false; } function checkID() { if (checkIDlength != 6) { PersonID = "Invalid ID Length" } document.getElementById('PersonID').innerHTML = PersonID if (NameID.indexOf('BR') === 0) {} else { PersonID = "Invalid ID Type" document.getElementById('PersonID').innerHTML = PersonID } if (isNaN(NameID.Length != 2)) { PersonID = "The Last 2 Digits must be numeric"; } else { PersonID = "OK" document.getElementById('PersonID').innerHTML = PersonID } } } 
 <!DOCTYPE html> <html> <body> <form name="Cars"> <h1>Car Sales</h1> <p>Enter Name</p> <input type="text" style="text-transform: uppercase" name="Name"><br> <p id="IDName"></p> <p>Enter ID</p> <input type="text" name="NameID"><br> <p id="PersonID"></p> <br> <button onclick="return begin()">Calculate</button> </form> </body> 

Looks like I've got this working by adding some return false into your if statements for the IDCheck function I'm using "BR1231" as my test case, I'm not sure if that is 100% valid but here is my code: 看起来我已经通过在IDCheck函数的if语句中添加了一些返回false来使用“BR1231”作为我的测试用例,我不确定这是否100%有效,但这是我的代码:

    function begin() {
  var Name = document.forms["Cars"]["Name"].value;
  var NameID = document.forms["Cars"]["NameID"].value;
  var IDName = "";
  var PersonID = "";

var checklength = Name.length;
CheckName();
console.log(checklength, checklength < 100)
var checkIDlength = NameID.length;
checkID();
console.log(checkIDlength, checkIDlength < 100)
return false;


function CheckName(){
  if (checklength >0 && checklength <21)
  {
IDName = "Ok"
  }
  else{
    IDName = "Invalid Name Length";
  }

  document.getElementById('IDName').innerHTML = IDName;
  return false;
}


function checkID(){
  if (checkIDlength !=6)
  {
    PersonID = "Invalid ID Length"

 document.getElementById('PersonID').innerHTML = PersonID
 return false;
}

  if (NameID.indexOf('BR') === 0) {
  }
  else {
    PersonID = "Invalid ID Type"
   document.getElementById('PersonID').innerHTML = PersonID
     return false;

}
   if (isNaN(NameID.Length !=2))
  {
   PersonID = "The Last 2 Digits must be numeric";
     return false;

 }
 else{
    PersonID = "OK"
    document.getElementById('PersonID').innerHTML = PersonID

 }

}
}

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

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