最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery get value from hidden input in column - Stack Overflow

programmeradmin4浏览0评论

I have got the following HTML table...

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

How can I get with jQuery the value of the hidden input field, when the link is clicked?

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

I have got the following HTML table...

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

How can I get with jQuery the value of the hidden input field, when the link is clicked?

$(".info").click(function() {
  // Here I need to find out the value...
});
Share Improve this question edited Jul 29, 2013 at 18:47 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jul 29, 2013 at 18:45 Unknown SoldierUnknown Soldier 511 silver badge4 bronze badges 1
  • jsfiddle/5DEa9 – PiLHA Commented Jul 29, 2013 at 18:54
Add a ment  | 

2 Answers 2

Reset to default 3

Here's how you do it :

$(".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 method will search for the previous element, which is where your input[hidden] is.

And its href, not hre, in the <a/> tags.

You can also use attributes <a href="#" data-hidden="1" class="info"> don't need use hidden fields

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

评论列表(0)

  1. 暂无评论