我想得到一个表单元格的值通过点击一个按钮在同一行的这个单元格。这个单元格是在前一列的按钮的列,这是我的尝试代码:
<table class="table table-bordered table-hover" >
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CategoryId)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category.CategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
<div class="btn-group-sm">
<button class="btn btn-primary" onclick="addArticle(this)">Add</button>
<button class="btn btn-warning">Edit</button>
</div>
</td>
</tr>
}
</tbody>
</table>
联森:
function addArticle(btn) {
var RowNumber = btn.previousSibling.innerHTML;
alert(RowNumber);
}
这段代码不起作用,显示未定义.有帮助吗?
1条答案
按热度按时间6qftjkof1#
您需要遍历DOM树:
1.表格单元格上一个同级