全局变量在if语句JS之外不可见

Global variable not not visible outside of if statement JS

本文关键字:JS if 语句 全局变量      更新时间:2023-09-26

努力理解为什么我的全局变量大量存在一个&abunbancetwo不会显示在if语句之外的控制台中?如果我在if语句内部console.log,但在if nothing之外?对于每个变量,我应该看到一个介于1-4之间的整数。

$('#selector1').change(function() {
  var selector1 = $('#selector1').val()
  if (selector1 % 1 == 0) {
    if (!selector1) {
      $('#ValOne').val('');
      abundanceone = 0;
      console.log (abundanceone);
      return false;
    }
    if (selector1 == 0) {
      $('#ValOne').val('');
      $('#selector1').val('');
      abundanceone = 0;
       console.log (abundanceone);
      return false;
    }
    if (selector1 >= 1 && selector1 <= 9) {
      $('#ValOne').val(1);
      abundanceone = 1;
      return false;
    }
    if (selector1 >= 10 && selector1 <= 99) {
      $('#ValOne').val(2);
      abundanceone = 2;
       console.log (abundanceone);
      return false;
    }
    if (selector1 >= 100 && selector1 <= 999) {
      $('#ValOne').val(3);
      abundanceone = 3;
       console.log (abundanceone);
      return false;
    }
    if (selector1 >= 1000) {
      $('#ValOne').val(4);
      window.abundanceone = 4;
 
      return false;
    }
  } else {
    alert('Please only enter a whole number');
  }
      console.log (abundanceone +99);
        console.log (window.abundanceone +999999);
});
$('#selector2').change(function() {
  var selector2 = $('#selector2').val()
  if (selector2 % 1 == 0) {
    if (!selector2) {
      $('#Valtwo').val('');
      abundancetwo = 0;
      return false;
    }
    if (selector2 == 0) {
      $('#Valtwo').val('');
      $('#selector2').val('');
      abundancetwo = 0;
      return false;
    }
    if (selector2 >= 1 && selector2 <= 9) {
      $('#Valtwo').val(1);
      abundancetwo = 1;
      return false;
    }
    if (selector2 >= 10 && selector2 <= 99) {
      $('#Valtwo').val(2);
      abundancetwo = 2;
      return false;
    }
    if (selector2 >= 100 && selector2 <= 999) {
      $('#Valtwo').val(3);
      abundancetwo = 3;
      return false;
    }
    if (selector2 >= 1000) {
      $('#Valtwo').val(4);
      abundancetwo = 4;
      return false;
    }
  } else {
    alert('Please only enter a whole number');
  }
  console.log(abundancetwo);
});
alert(abundanceone + abundancetwo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="categorySelect1fromDB" id="ValOne" value="Abundance" class="DropChange" disabled>
<br>
<input maxlength="4" type="text" placeholder="Estimated Quantity" id="selector1" name="number_of_taxon1" />
<br>
<br>
<input type="text" name="categorySelect1fromDB" id="Valtwo" value="Abundance" class="DropChange" disabled>
<br>
<input maxlength="4" type="text" placeholder="Estimated Quantity" id="selector2" name="number_of_taxon1" />

我显式地全局声明了您的变量,并且它有效。

看一看。https://jsfiddle.net/3tnzkmnc/

var abundanceone =0;
var abundancetwo =0;

关于if loop之外的控制台日志,代码永远不会到达那里,因为每个if condition中都有return statements。它到达if loop之外的控制台日志的唯一时间是当它进入else condition code时,但此时您没有设置变量以查看控制台日志中的任何内容。

abundanceoneabuancetwo封装在函数中,因此在调用函数之前不会定义它们。一旦调用了这些函数,您就可以全局访问这些变量,或者您可以在function之外声明这些变量。