javascript - 如何获取表中 TR 中 TD 的大陆值?

标签 javascript jquery razor

我想获取我点击的td的值 我有一张 table ,在这张 table 上我有很多 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 中的链接

最佳答案

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

示例:

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

关于javascript - 如何获取表中 TR 中 TD 的大陆值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37318417/

相关文章:

javascript - 使用 jquery 验证插件验证 Jquery 自动完成字段

php - JQuery .post - $_POST 为空

razor - 在 Razor 中将日期转换为 ToShortDateString

javascript - 从对话框中的文本框获取旧值

javascript - 405 方法不允许 - 适用于除 iOS Safari 之外的所有内容

javascript - Node.js 类模块系统

javascript - HTML 图像未加载(自动添加内联 css 和属性)

Jquery - 模拟点击按钮不触发功能

c# - 在 Razor View 引擎中将 HTML 元素作为 Function<> 传递

asp.net-mvc - 如何使用 MVC4 和 razor View 在 LoginPartial 页面中显示自定义字段