En mi aplicación, he cargado cierta información en la tabla.
Ahora quiero obtener la identificación del elemento de esa fila cuando el usuario hace clic.
Aquí Item.Id es el que quiero obtener, pero aquí no lo asigné a ninguna fila de la tabla. (Pensé que no es necesario).
Hasta ahora también he creado el JavaScript, pero estoy atascado en obtener la identificación en el evento de click .
Puedes ayudarme aqui?
Así es como lo hice hasta ahora:
<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>Este es el guión. Se activa cuando se hace clic, pero quiero obtener la identificación del artículo cuando el usuario hace clic en una fila determinada.
<script type="text/javascript"> $("#tblParts tr").click(function (event) { alert("Row Clicked"); }); </script>Para obtener la identificación del objetivo, debe hacer esto en el código de clic y, si tiene una identificación, la tomará
function(event){ alert(event.target.id) }Una de las soluciones es:
<script type="text/javascript"> $("#tblParts tr").click(function (event) { var cell = this.getElementsByTagName("td")[0]; alert("PartNo: " + cell.innerText); }); </script>