在我的應用程式中,我已將一些資訊加載到表中。
現在我想在用戶點擊時獲取該行的專案 ID。
這里的 Item.Id 是我想要獲取的,但這里我沒有分配給任何表格行。(我認為它不需要)。
到目前為止,我還創建了 JavaScript,但我在click事件中無法獲取 id 。
你能幫我嗎?
這是我到目前為止所做的:
<div class="table-full-width table-responsive">
<table class="table" id="tblParts">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().PartNo)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartModel)
</th>
<th>
@Html.DisplayNameFor(model => model.First().AvaQty)
</th>
<th>
@Html.DisplayNameFor(model => model.First().ReOrderQty)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PartCatogary)
</th>
<th>
@Html.DisplayNameFor(model => model.First().LastUpdated)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartNo)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartDescription)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartModel)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.AvaQty)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.ReOrderQty)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.PartCatogary)
</td>
<td>
<a href="#">
@Html.DisplayFor(modelItem => item.LastUpdated)
</td>
</tr>
}
</table>
這是腳本。它在單擊時觸發,但我想在用戶單擊特定行時獲取專案 ID。
<script type="text/javascript">
$("#tblParts tr").click(function (event) {
alert("Row Clicked");
});
</script>
uj5u.com熱心網友回復:
解決方案之一是:
<script type="text/javascript">
$("#tblParts tr").click(function (event) {
var cell = this.getElementsByTagName("td")[0];
alert("PartNo: " cell.innerText);
});
</script>
uj5u.com熱心網友回復:
要從目標獲取 id,您必須在點擊代碼上執行此操作,如果它有一個 id,它將抓取它
function(event){
alert(event.target.id)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/375269.html
標籤:javascript C# asp.net-mvc 剃刀
