簡體   English   中英

jQuery從列中的隱藏輸入中獲取價值

[英]jQuery get value from hidden input in column

我有以下HTML表格...

<table>
  <thead>
    <tr>
      <th>Nr.</th>
      <th>Name</th>
      <th>Info</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Laura</td>
      <td><input type="hidden" value="1"><a href="#" class="info">Info</a></td>
    </tr>
    <tr>
      <td>2</td>
      <td>Sabrina</td>
      <td><input type="hidden" value="2"><a href="#" class="info">Info</a></td>
    </tr>
  </tbody>
</table>

單擊鏈接后,如何使用jQuery獲得隱藏輸入字段的值?

$(".info").click(function() {
  // Here I need to find out the value...
});

這是您的操作方式:

$(".info").click(function(e) {
  //just in case, will be useful if your href is anything other than #
  e.preventDefault();
  alert($(this).prev('input[type="hidden"]').val());
});

prev方法將搜索上一個元素,即您的input[hidden]所在的位置。

及其<a/>標記中的href而不是hre

您還可以使用屬性<a href="#" data-hidden="1" class="info">不需要使用隱藏字段

$(".info").click(function(e) {
  e.preventDefault();
  alert($(this).data('hidden')); // or $(this).attr('data-hidden');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM