简体   繁体   English

我如何在for循环中设置一个javascript变量供以后使用

[英]How do I set a javascript variable inside a for loop to be used afterwards

So, I'm creating a login form, and when certain criteria aren't met to continue after the form, I am setting a variable to be tested after I've tested all the criteria. 因此,我正在创建一个登录表单,当不满足某些条件才能在表单之后继续时,我将在测试所有条件之后设置要测试的变量。 IF that variable ($cantcontinue) is set to 'true' I want to send a console message with the criteria that isn't met. 如果该变量($ cantcontinue)设置为'true',我想发送一条控制台消息,其中包含不符合的条件。 Here is my code: 这是我的代码:

  function testfields() {
    // Ask if logging in or Creating Account
    //Logging In:
    if (document.getElementById("tEmail").style.display != "unset") {
      var loginelements = ["Username", "Password"];

      var text = "";
      var i;
      for (i = 0; i < loginelements.length; i++) {

        //Check all fields are full:
        if (document.getElementById(loginelements[i]).value == "") {
          document.getElementById(loginelements[i]).style.background = '#ff6060'
          var cantcontinue = true;
          console.log(loginelements[i] + " is not set,")
        } else {
          document.getElementById(loginelements[i]).style.background = '#f7f7f7'
        }
      }
      if ($cantcontinue != true) {
        console.log("Create Account")
      } else {
        console.log("Could Not Create Account")
      }



    //Create a new Account:
    } else {
      var createelements = ["Username", "Password", "tEmaili", "tConfirmi"];

      var text = "";
      var i;
      for (i = 0; i < createelements.length; i++) {

        //Check all fields are full:
        if (document.getElementById(createelements[i]).value == "") {
          document.getElementById(createelements[i]).style.background = '#ff6060'
          var cantcontinue = true;
          console.log(createelements[i] + " is not set,")
        } else {
          document.getElementById(createelements[i]).style.background = '#f7f7f7'
        }
      }

      //If passwords Match
      if (document.getElementById("Password").value != document.getElementById("tConfirmi").value) {
        var cantcontinue = true;
        document.getElementById("tConfirmi").style.background = '#ff6060'
        document.getElementById("tConfirmi").value = ''
        console.log(" Passwords didn't Match,");
      }

      if ($cantcontinue != true) {
        console.log("Create Account")
      } else {
        console.log("Could Not Create Account")
      }
    }        
  }
$cantcontinue !== cantcontinue;//....

Also

if (cantcontinue != true) {

equals: 等于:

if (!cantcontinue) {

Sidenote: Please don't use "$" unless you're using jQuery. 旁注:除非您使用的是jQuery,否则请不要使用“ $”。 It reminds me of PHP ( brrrrr...) 它让我想起了PHP(brrrrr ...)

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

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