简体   繁体   English

从 html 表中获取值

[英]get values from an html table

I need to write a website that calculates the inverse matrix.我需要编写一个计算逆矩阵的网站。 I have a table.我有一张桌子。 I also wrote a function to resize the table.我还写了一个函数来调整表格的大小。 I need the entered values to be read into a two-dimensional array (arr), and I can use this array as a function argument (PrintMatrix(arr)).我需要将输入的值读入二维数组 (arr),并且可以将此数组用作函数参数 (PrintMatrix(arr))。 I'm new to this and I'm not very good at it我是新手,我不太擅长

My table:我的桌子:

<div class="row col-sm-12" id="block1">
  <div class="well well-sm col-xs-12 col-lg-11">
      <h4><b>Inverse Matrix</b></h4>
   
      <div class="row">
          <div class="col-sm-12">
              <form class="form-inline" autocomplete="off" id ="mainForm">
                  <div class="input-group">
                      <span class="input-group-addon">Size:</span>
                      <select id="mrow" onchange=" CreateTable(); " class="form-control">
                          <option value="2">2x2</option>
                          <option selected value="3">3x3</option>
                          <option value="4">4x4</option>
                          <option value="5">5x5</option>
                          <option value="6">6x6</option>
                          <option value="7">7x7</option>
                      </select>
                  </div>
              
              </form>
        
              <form autocomplete="off" id="form1">
                  <div class="row">
                      <div class="col-xs-1">
                          <h3 class="text-center">A</h3>
                      </div>
                      <div class="col-xs-9 col-sm-6" id="divA">
                          <table class="table_matrix" id="matrixA">
                              <tbody>
                                  <tr>
                                    <td><input id="a11" type="text" /></td>
                                    <td><input id="a12" type="text" /></td>
                                    <td><input id="a13" type="text" /></td>
                                  </tr>
                                  <tr>
                                    <td><input id="a21" type="text" /></td>
                                    <td><input id="a22" type="text" /></td>
                                    <td><input id="a23" type="text" /></td>
                                  </tr>
                                  <tr>
                                    <td><input id="a31" type="text" /></td>
                                    <td><input id="a32" type="text" /></td>
                                    <td><input id="a33" type="text" /></td>
                                  </tr>
                              </tbody>
                          </table>
                      </div>
                  </div>
            
              </form>
              
          </div>
      </div>
  </div>
</div>

function for resizing:调整大小的功能:

function CreateTable() {
        var element = document.getElementById("matrixA");
        element.parentNode.removeChild(element);
        var columnA = document.getElementById("mrow").value;
        var rowA = document.getElementById("mrow").value;
        var doc = document;

        var fragment1 = doc.createDocumentFragment();
        for (var i = 1; i <= rowA; i++) {
            var tr = doc.createElement("tr");
            for (var j = 1; j <= columnA; j++) {
                var td = doc.createElement("td");
                td.innerHTML = "<input id='a" + i + j + "' type='text'/>";
                tr.appendChild(td);
            }
            fragment1.appendChild(tr);
        }
        var table1 = doc.createElement("table");
        table1.className = "table_matrix";
        table1.id = "matrixA";
        table1.appendChild(fragment1);
        doc.getElementById("divA").appendChild(table1);
    }

Also, i have it, but it only works for a 3x3 table and if you change the size of the table, you still get a 3x3 array.另外,我有它,但它只适用于 3x3 表,如果你改变表的大小,你仍然会得到一个 3x3 数组。 Also, for some reason, the array values are "undefined"此外,由于某种原因,数组值是“未定义的”

var table = document.querySelector('#matrixA');
  var tbody = table.querySelector('tbody');
  var tr = tbody.querySelectorAll('tr');
  let tableArray = new Array();
  
  for (let i = 0; i < tr.length; i++) {
  tableArray[i] = new Array();
  for (let j = 0; j < tr[i].children.length; j++) {
    tableArray[i][j] = (tr[i].children[j].children.value);
  }
  }
  console.log(tableArray);

If you want a function that returns a matrix with the input of your user, based on your code, you can use a more simple approach using the id s that you've set, with something like this:如果您想要一个根据您的代码返回包含用户输入的矩阵的函数,您可以使用更简单的方法,使用您设置的id ,如下所示:

function getInputByMatrix() {
    const rows = document.getElementById('mrow').value;
    let input = [];
    for (let i = 1; i <= rows; i++) {
        input[i-1] = [];
        for (let j = 1; j <= rows; j++) {
            input[i-1][j-1] = document.getElementById(`a${i}${j}`).value;
        }
    }
    return input;
}

For what concern your attempt to create this function, you get only undefined values because you are attempting to get the value property on a HTMLCollection object;对于您尝试创建此函数的问题,您只会获得undefined的值,因为您正在尝试获取HTMLCollection对象的value属性; indeed the children read-only property returns, as you can see from the following quote from the docs , a live collection of children elements.实际上, children只读属性返回,正如您从docs的以下引用中看到的那样,它是子元素的实时集合。

The read-only children property returns a live HTMLCollection which contains all of the child elements of the element upon which it was called.只读 children 属性返回一个活动的 HTMLCollection,其中包含调用它的元素的所有子元素。

An HTMLCollection which is a live, ordered collection of the DOM elements which are children of node.一个 HTMLCollection,它是一个活动的、有序的 DOM 元素集合,它们是 node 的子元素。 You can access the individual child nodes in the collection by using either the item() method on the collection, or by using JavaScript array-style notation.您可以通过使用集合上的 item() 方法或使用 JavaScript 数组样式表示法来访问集合中的各个子节点。

If the element has no element children, then children is an empty list with a length of 0.如果该元素没有子元素,则 children 是一个长度为 0 的空列表。

To obtain the desired behavior, in this case, you could just access the first element of the collection, that is tableArray[i][j] = (tr[i].children[j].children[0].value);要获得所需的行为,在这种情况下,您只需访问集合的第一个元素,即tableArray[i][j] = (tr[i].children[j].children[0].value); . .

Also, when resizing the matrix, you forgot to insert a tbody tag, so your function will always fire an exception when the size of the matrix has been changed.此外,在调整矩阵大小时,您忘记插入tbody标记,因此当矩阵大小发生​​更改时,您的函数将始终触发异常。

var fragment1 = doc.createDocumentFragment();
    // There is no <tbody>!
    for (var i = 1; i <= rowA; i++) {
        var tr = doc.createElement("tr");
        for (var j = 1; j <= columnA; j++) {
            var td = doc.createElement("td");
            td.innerHTML = "<input id='a" + i + j + "' type='text'/>";
            tr.appendChild(td);
        }
        fragment1.appendChild(tr);
    }
    var table1 = doc.createElement("table");
    table1.className = "table_matrix";
    table1.id = "matrixA";
    // Appending to the <table> tag the fragment composed by <tr> <td> ... </td> </tr>
    table1.appendChild(fragment1);
    doc.getElementById("divA").appendChild(table1);

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

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