如何在表中的TR中获得TD的竞争价值? [重复]

时间:2022-06-09 09:20:30

This question already has an answer here:

这个问题在这里已有答案:

I want to get the value of the td I click I have a table ,in this table I have many tr and td I want to get the value of the td I selected

我想得到td的值我点击我有一个表,在这个表中我有很多tr和td我想得到我选择的td的值

如何在表中的TR中获得TD的竞争价值? [重复]

<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>

i want to pass the value of dem_dpr into the link in javascript

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

2 个解决方案

#1


0  

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

For every TD, bind click function in each of them. .text() function will get only the text in TD out from it. It is best if you could put ID for the table. So if you have added ID to the table. The solution would be like this:

对于每个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");
   });
  });

#2


2  

First of all, you cannot use IDs this way. They need to be unique per document. Use class instead of IDs. Then, you can use .closest('.iddem') to grab the element closest to the link you clicked and use .html() or .text() to grab it's value.

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

Example:

<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");
   });
  });

For every TD, bind click function in each of them. .text() function will get only the text in TD out from it. It is best if you could put ID for the table. So if you have added ID to the table. The solution would be like this:

对于每个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");
   });
  });

#2


2  

First of all, you cannot use IDs this way. They need to be unique per document. Use class instead of IDs. Then, you can use .closest('.iddem') to grab the element closest to the link you clicked and use .html() or .text() to grab it's value.

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

Example:

<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>