单击图像时获取td列值

时间:2013-01-29 20:07:47

标签: jquery asp.net-mvc razor

我在razor代码中有一个表,其中包含使用表中每条记录呈现的标记。当用户点击此图片时,我很难找到方法,app会针对点击的行(在jquery中)和警报ID获取商店ID。

<table id="grid">
 <thead> 
 <tr>
    <th data-field="store_id">Store ID</th>
    <th data-field="address">Address</th>
    <th data-field="postcode">post Code</th>
    <th data-field="city">City</th>
    <th data-field="country">Country</th>
    <th data-sortable="false">Action</th>
   </tr>
  </thead>

 <tbody>
 @foreach (var item in Model)
 {
   <tr>
    <td>@Html.DisplayFor(modelItem=> item.storeID)</td>
    <td>@Html.DisplayFor(modelItem=> item.address)</td>
    <td>@Html.DisplayFor(modelItem=> item.postcode)</td>
    <td>@Html.DisplayFor(modelItem=> item.city)</td>
    <td>@Html.DisplayFor(modelItem=> item.Country)</td>
    <td><img class="img" src="~/Images/delete.png" /></td>
   <!-- <td> <input type="button" class="b1" value ="Press Me" /></td>-->
  </tr>
  }
 </tbody>
</table>

这是使用jQuery的javascript代码

<script>

 $(document).ready(function () {

     $(".img").click(function () {

         var a = $(this).  ?????

         alert("clicked " + a);

         var store = { storeID: '3' }
         $.ajax({
             type: "POST",
             url: "/Master/processJsonRequest",
             data: JSON.stringify({ model: store }),
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             success: function (response) {
                 alert(response);
             }
         });
     });
 });



</script>

1 个答案:

答案 0 :(得分:0)

试试这个:

$(".img").click(function () {
    var storeId = $(this).closest('tr').find('td:first').text();
    alert("clicked " + storeId);

    // fire off your ajax...
});

Example fiddle

相关问题