Получить значения из 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> |
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, что я делаю не так? |
failoflife,
5 строка ненужна строка 24 должна быть в строке 9ж строка 19 у вас от фонаря написана -- значение первого инпута во все ячейки. и если пусто в инпуте то NaN и будет |
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> |
Цитата:
Цитата:
|
Цитата:
|
Цитата:
|
Часовой пояс GMT +3, время: 11:00. |