如何在表中的TR中获得TD的竞争价值?

时间:2016-05-19 08:56:12

标签: javascript jquery razor

我想得到我点击的td的值 我有一张桌子,在这张桌子里我有很多tr和td 我想获得我选择的td的值

enter image description here

<table id="table" class="table" style="margin-right: auto; margin-left: auto" >
    <thead>
      <tr>
        <th>Numero demande</th>
        <th>Date prelevement</th>
        <th>Um executante</th>
        <th>Id preleveur</th>

      </tr>
    </thead>
    <tbody>



@foreach(var dem in @Model) 
      {

        <tr>
        <td><a id="lienFicheDemande"> @dem.DPR</a></td>
        <td>@dem.Dateprelevement  </td>
        <td>@dem.UM  </td>
        <td>@dem.PRELEVEUR.NOMCOMPLET </td>
        <td id="iddem" hidden="hidden">@dem.DPR<</td>
        </tr>
      }

    </tbody>
  </table>
</body>
<script type="text/javascript" >



        $(document).ready(function (e) {

            $('#lienFicheDemande').click(function (e) {

                alert($('#iddem')[0].innerHTML);


                window.open("Appli/Home/FicheDemande?iddem=" + $('#iddem').value, "nom_popup", " menubar=no");

            });
        });

    </script>

我想将dem_dpr的值传递给javascript中的链接

2 个答案:

答案 0 :(得分:2)

首先,您不能以这种方式使用ID。它们需要在每个文档中都是唯一的。使用class而不是ID。然后,您可以使用.closest('.iddem')抓取最靠近您点击的链接的元素,并使用.html().text()来获取它的值。

示例:

<table id="table" class="table" style="margin-right: auto; margin-left: auto" >
    <thead>
      <tr>
        <th>Numero demande</th>
        <th>Date prelevement</th>
        <th>Um executante</th>
        <th>Id preleveur</th>

      </tr>
    </thead>
    <tbody>



@foreach(var dem in @Model) 
      {

        <tr>
        <td><a class="lienFicheDemande"> @dem.DPR</a></td>
        <td>@dem.Dateprelevement  </td>
        <td>@dem.UM  </td>
        <td>@dem.PRELEVEUR.NOMCOMPLET </td>
        <td class="iddem" hidden="hidden">@dem.DPR<</td>
        </tr>
      }

    </tbody>
  </table>
</body>
<script type="text/javascript" >



        $(document).ready(function (e) {

            $('.lienFicheDemande').click(function (e) {

                alert($(this).closest('.iddem').html());


                window.open("Appli/Home/FicheDemande?iddem=" + $(this).closest('.iddem').html(), "nom_popup", " menubar=no");

            });
        });

    </script>

答案 1 :(得分:0)

 $(document).ready(function () {
  $('td').click(function () {
   window.open("Appli/Home/FicheDemande?iddem=" + $(this).text(), "nom_popup"," menubar=no");
   });
  });

对于每个TD,在每个TD中绑定单击功能。 .text()函数只能从中获取TD中的文本。 最好是你可以为表格放置ID。 因此,如果您已向表中添加了ID。解决方案是这样的:

$(document).ready(function () {
  $('#table_id td').click(function () {
   window.open("Appli/Home/FicheDemande?iddem=" + $(this).text(), "nom_popup"," menubar=no");
   });
  });