Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Получить значения из inputов в матрицу (https://javascript.ru/forum/jquery/62322-poluchit-znacheniya-iz-inputov-v-matricu.html)

failoflife 04.04.2016 21:21

Получить значения из inputов в матрицу
 
Есть таблица inputов, хочу получить введенных в них значения и записать в матрицу.

<table>
                        <tbody>
                        <tr>
                            <td><input type="text"></td>
                            <td><input type="text"></td>
                        </tr>
                        <tr>
                            <td><input type="text"></td>
                            <td><input type="text"></td>
                        </tr>
                        <tr>
                            <td><input type="text"></td>
                            <td><input type="text"></td>
                        </tr>
                        </tbody>
                    </table>

рони 04.04.2016 21:32

failoflife,
и в чём проблема?

failoflife 07.04.2016 17:40

Цитата:

Сообщение от рони
failoflife,
и в чём проблема?

<button id="rowCount">Count 'Em</button>
<br />
<table id="myTable">
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>

</table>


$(function() {
    var $rows = $('table#myTable tr:last').index() + 1;
    var $columns = $("table").find("tr:first td").length;
    $('#rowCount')
        .button()
        .click(function() {
            alert("Rows = " + $rows);
            alert("Cols = " + $columns);
            alert("Matrix = " + myMatrix);

        });


function matrixArray(rows, columns) {
  var arr = new Array();
  for (var i = 0; i < columns; i++) {
    arr[i] = new Array();
    for (var j = 0; j < rows; j++) {
      arr[i][j] = parseInt($('input').val()); 
    }
  }
  return arr;
}
var myMatrix = matrixArray($rows, $columns);
});


матрица имеет значение NaN, что я делаю не так?

рони 07.04.2016 17:59

failoflife,
5 строка ненужна
строка 24 должна быть в строке 9ж
строка 19 у вас от фонаря написана -- значение первого инпута во все ячейки.
и если пусто в инпуте то NaN и будет

рони 07.04.2016 18:03

failoflife,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

  <script>
$(function() {
    var $rows = $("#myTable tr");
    $("#rowCount").click(function() {
        alert("Matrix = " + JSON.stringify(matrixArray($rows)))
    });

    function matrixArray(rows) {
        return $.map(rows, function(tr) {
            return [$.map($("input", tr), function(input) {
                return +input.value || 0
            })]
        })
    }
});
  </script>
</head>

<body>
<button id="rowCount">Count 'Em</button>
<br />
<table id="myTable">
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
    <td>
      <input type="text">
    </td>
  </tr>

</table>

</body>
</html>

failoflife 07.04.2016 18:08

Цитата:

Сообщение от рони
строка 19 у вас от фонаря написана -- значение первого инпута во все ячейки.

А как тогда сделать так, при заполнении матрицы заполнялись значения из инпутов в таблице?
Цитата:

Сообщение от рони
и если пусто в инпуте то NaN и будет

Но, я же ведь перед нажатием кнопки ввожу в инпуты значения.

failoflife 07.04.2016 18:09

Цитата:

Сообщение от рони
рони рони на форуме

СПАИБОООО! Щас буду разбираться.

рони 07.04.2016 19:00

Цитата:

Сообщение от failoflife
Но, я же ведь перед нажатием кнопки ввожу в инпуты значения.

неа -- строка 24 исполнится до вашего заполнения


Часовой пояс GMT +3, время: 11:00.