如何从输入中获取数据并使用 jQuery 将它们放入数组中?
How to get data from inputs and put them in an array using jQuery?
我使用 'for' 循环生成一个 9X9(行 = 9;列 = 9)table(它有 81 个单元格)。
我还有一个整数数组Model.grid[9, 9],其中45个随机元素为0,其余为1到9的个位数。
然后我填充table的单元格,如下所示:如果匹配的grid[]元素是'0',那么我在当前单元格中放置一个输入框;如果匹配的 grid[] 元素不同于“0”(从 1 到 9 的数字),那么我用匹配的 grid[] 元素填充单元格。
所以代码看起来像这样:
1) 可视化 table:
@model Generator.GeneratorClass
@helper drawCells(int row)
{
for (int i = 0; i < Model.cols; i++)
{
if(Model.grid[row, i] == 0)
{
<td>
<input type="text" maxlength="1"/>
</td>
}
else
{
<td>
@Model.grid[row, i].ToString()
</td>
}
}
}
@helper drawRows()
{
for (int i = 0; i < Model.rows; i++)
{
<tr>@drawCells(i)</tr>
}
}
2) HTML:
<div id="puzzle">
<table border="1" id="puzzle-table">
@drawRows()
</table>
</div>
<button onclick="check()">
Check
</button>
到目前为止一切正常。用户最终应填写 table 的 45 个空单元格。然后我希望在用户单击 table 下方的 'Check' 按钮后,check() 函数创建一个新的数组字段 [] 并用输入框中的值填充它。我用了这个脚本,但是它不起作用。
3) 脚本(不起作用):
function check() {
var fields = [];
$('#puzzle-table input').each(function (i) {
fileds.push(i);
});
如何使用 jQuery 从输入框中获取值并将它们放入数组字段[]?
<script type="text/javascript">
function check() {
var fields = [];
$('#puzzle-table input').each(function () {
fileds.push( $(this).val() );
});
}
</script>
我使用 'for' 循环生成一个 9X9(行 = 9;列 = 9)table(它有 81 个单元格)。
我还有一个整数数组Model.grid[9, 9],其中45个随机元素为0,其余为1到9的个位数。
然后我填充table的单元格,如下所示:如果匹配的grid[]元素是'0',那么我在当前单元格中放置一个输入框;如果匹配的 grid[] 元素不同于“0”(从 1 到 9 的数字),那么我用匹配的 grid[] 元素填充单元格。
所以代码看起来像这样:
1) 可视化 table:
@model Generator.GeneratorClass
@helper drawCells(int row)
{
for (int i = 0; i < Model.cols; i++)
{
if(Model.grid[row, i] == 0)
{
<td>
<input type="text" maxlength="1"/>
</td>
}
else
{
<td>
@Model.grid[row, i].ToString()
</td>
}
}
}
@helper drawRows()
{
for (int i = 0; i < Model.rows; i++)
{
<tr>@drawCells(i)</tr>
}
}
2) HTML:
<div id="puzzle">
<table border="1" id="puzzle-table">
@drawRows()
</table>
</div>
<button onclick="check()">
Check
</button>
到目前为止一切正常。用户最终应填写 table 的 45 个空单元格。然后我希望在用户单击 table 下方的 'Check' 按钮后,check() 函数创建一个新的数组字段 [] 并用输入框中的值填充它。我用了这个脚本,但是它不起作用。
3) 脚本(不起作用):
function check() {
var fields = [];
$('#puzzle-table input').each(function (i) {
fileds.push(i);
});
如何使用 jQuery 从输入框中获取值并将它们放入数组字段[]?
<script type="text/javascript">
function check() {
var fields = [];
$('#puzzle-table input').each(function () {
fileds.push( $(this).val() );
});
}
</script>