我有一個表格填充了來自該頁面的C#模型的一組TextBoxFor輸入。這些輸入的每一列都有不同的類名。在任何一個類上使用.change事件,使用已更改的單元格的行和單元格/列號來標識其正下方行中的單元格。但是,更改的單元格的值始終顯示爲未定義。我嘗試過搜索答案,.val(),.html(),.text(),獲取已更改的單元格ID並將.val()應用於此,但對已更改的單元格的任何引用都是未定義的。如何正確解決更改的單元格?提前謝謝了!參考表單元格的值使用jQuery
的jQuery:
$('.firstClass, .secondClass, .thirdClass, .fourthClass, .fifthClass').change(function() {
var $changedCell = $(this);
var $changedRow = $changedCell.parent().index();
var $cellIndex = $changedCell.index();
var $nextRow = $changedRow + 1;
var $table = $('#dt_myTable tr');
var $beneathCell = $table.eq($nextRow).find('td').eq($cellIndex);
var $changedValue = Number($changedCell.val());//Zero or Undefined, depending on using .val() or .html()/.text()!
$beneathCell.text($changedValue);
});
這裏的表的樣本:
<table id="dt_myTable" class="table table-bordered">
<thead>
<tr><th class="text-center">Suggested</th><th class="text-center">First</th><th class="text-center">Second</th><th class="text-center">Third</th><th class="text-center">Fourth</th><th class="text-center">Fifth</th></tr>
</thead>
<tbody>
<tr>
<td align="center">Primary</td>
<td class="firstClass" align="center">@Html.TextBoxFor(m => m.FirstPrimary, new { @class = "form-control", data_parsley_required = "true" })</td>
<td class="secondClass" align="center">@Html.TextBoxFor(m => m.SecondPrimary, new { @class = "form-control", data_parsley_required = "true" })</td>
<td class="thirdClass" align="center">@Html.TextBoxFor(m => m.ThirdPrimary, new { @class = "form-control", data_parsley_required = "true" })</td>
<td class="fourthClass" align="center">@Html.TextBoxFor(m => m.FourthPrimary, new { @class = "form-control", data_parsley_required = "true" })</td>
<td class="fifthClass" align="center">@Html.TextBoxFor(m => m.FifthPrimary, new { @class = "form-control", data_parsley_required = "true" })</td>
</tr>
<tr>
<td align="center"><b>Result</b></td>
<td class="firstClass" align="center"></td>
<td class="secondClass" align="center"></td>
<td class="thirdClass" align="center"></td>
<td class="fourthClass" align="center"></td>
<td class="fifthClass" align="center"></td>
</tr>
</tbody>
</table>
顯示你的html的一個例子。 –
針對以下答案添加。 – jle
感謝您的回答!這很奇怪,因爲它似乎確實獲得了單元格的索引。如果測試數據被放入$ beneathCell.text(「測試」)並且未定義的行被註釋掉,則它會正確更新。它似乎只是訪問更改的單元格及其值不起作用的值。是的,但目的是獲取已更改單元格的值,並使用相同的值直接更新它下面的單元格。 – jle