Проблема получения переменной массива JavaScript для таблицы HTML - PullRequest
0 голосов
/ 02 июля 2018

У меня проблема, я не смог получить две мои переменные (tabArray, currentValue) из моего кода здесь.

Итак, у меня есть первый скрипт наверху, который работает и управляет моей кнопкой и штрих-кодом. Когда я нажимаю, я должен показать новое значение tabArray в HTML. Мне нужно получить значение tabArray (выбранное значение) и currentValue (фактическая выбранная страница), чтобы построить мою таблицу HTML.

Как я мог получить их из сценария наверху?

<script type="text/javascript" src=".\js\jquery-1.8.3.min.js"></script>
<script type="text/javascript" src=".\jquery-barcode.js"></script>
<script type="text/javascript">
  var arrayValue = ["17723#", "17724#"];
  var tabArray = [
    ["30", "4697", "H021", "Insp.lot001", "Var TOTO"],
    ["301", "4697", "H021", "Insp.lot001", "Var TOTO"]
  ];
  var currentValue = 0;

  function buttonsControl() {
    if (currentValue == 0) {
      buttonPrev.disabled = true;
      buttonNext.disabled = false;
    } else if (currentValue > 0 && currentValue < (arrayValue.length - 1)) {
      buttonPrev.disabled = false;
      buttonNext.disabled = false;
    } else if (currentValue == (arrayValue.length - 1)) {
      buttonPrev.disabled = false;
      buttonNext.disabled = true;
    }
  }

  function generateBarcode() {
    var value = arrayValue[currentValue].toString();
    var btype = "_barcodeType_";
    var renderer = "bmp";
    var quietZone = false;
    var settings = {
      output: renderer,
      bgColor: "#FFFFFF",
      color: "#000000",
      moduleSize: "8"
    };
    $("#barcodeTarget").html("").show().barcode(value, btype, settings);
    barcodeCurrent.innerHTML = arrayValue[currentValue].toString();
    nOFm.innerHTML = (currentValue + 1) + " of " + arrayValue.length;
    buttonsControl();
  }

  function goPrev() {
    if (currentValue > 0) {
      currentValue = currentValue - 1;
    } else {
      currentValue = 0;
    }
    generateBarcode();
  }

  function goNext() {
    if (currentValue < (arrayValue.length - 1)) {
      currentValue = currentValue + 1;
    } else {
      currentValue = arrayValue.length - 1;
    }
    generateBarcode();
  }

  $(function() {
    generateBarcode();
  });
</script>
</head>

<body expr:class='&quot;loading&quot; + data:blog.mobileClass' oncontextmenu='return false;' onkeydown='return false;' onmousedown='return false;' style='-moz-user-select: none;'>
  <button id="buttonPrev" onclick="goPrev()">Previous</button>
  <button id="buttonNext" onclick="goNext()">Next</button>
  <div id="barcodeTarget"></div>
  <p id="barcodeCurrent"></p>
  <p> </p>
  <p id="nOFm"> </p>

  <table id="table" border="1">
    <tr>
      <th>ID</th>
      <th>TEST</th>
      <th>BAT</th>
      <th>Ins</th>
      <th>Var</th>
    </tr>
  </table>
  <script>
    var tabArray = obj.getElementsByTagName("tabArray")[0].value,
      table = document.getElementById("table");
    var NewLine = obj.getElementsByTagName("currentValue")[0].value;
    //for(var i = currentValue; i < array.length; i++)
    //
    // create a new row
    var newRow = table.insertRow(table.length);
    for (var j = 0; j < array[NewLine].length; j++) {
      // create a new cell
      var cell = newRow.insertCell(j);

      // add value to the cell
      cell.innerHTML = array[NewLine][j];
    }

    document.innerHTML = currentValue;
  </script>
</body>
</html>

1 Ответ

0 голосов
/ 02 июля 2018

Нижний тег script имеет доступ ко всем переменным из верхней - переменные являются глобальными.

Я думаю, вы случайно перезаписываете tabArray, когда пишете var tabArray = obj.getElementsByTagName..., поэтому переменная становится нулевой.

Замена вашего второго блока кода на это даст то, что вы хотите сделать:

var table = document.getElementById("table");

for (var i = 0; i < tabArray.length; i++)
{
    // create a new row
    var newRow = table.insertRow(table.length);
    for (var j = 0; j < tabArray[i].length; j++) {
      // create a new cell
      var cell = newRow.insertCell(j);

      // add value to the cell
      cell.innerHTML = tabArray[i][j];
    }
}
document.innerHTML = currentValue;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...