繁体   English   中英

变量在Java for循环中未存储超过一秒钟

[英]Variable not being stored for than a second in Javascript for loop

在开始之前,我知道StackOverflow中还有许多其他文章,这些文章没有使用Javascript语言存储变量。 但是,解决这些问题的方法主要是简单的,例如在循环外声明变量。 不幸的是,在我看来,这是行不通的。 这是我的代码:

function authenticationselection() {
  var whichformofauthentication =
    document.getElementsByName("authenticationtypes")
  var len = whichformofauthentication.length
  for (i = 0; i < len; i++) {
    if (whichformofauthentication[i].checked) {
      waytoauthenticate = whichformofauthentication[i].value
      console.log(waytoauthenticate)
      break
    } else {
      continue
    }
  }
}

我已经在foor循环之外声明了变量waytoauthenticate,这意味着它是一个全局变量。 当我运行程序的这一部分时,变量waytoauthenticate设置为单选按钮的值(这是此代码的作用)。 但是,变量waytoauthenticate仅定义为一秒钟的值。 经过非常短的时间后,变量将变为其原始值,该值为空。 谁能帮我弄清楚为什么只将变量定义为单选按钮的值一秒钟。 谢谢!

您将在for循环中再次声明变量var waytoauthenticate = whichformofauthentication [i] .value。 尝试删除已在外部声明的var。 希望这可以帮助

暂无
暂无

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

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