jQuery从列中的隐藏输入中获取价值
问题描述:
我有以下HTML表格...
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>
当单击链接时,如何使用jQuery获得隐藏输入字段的值?
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...
});
答
操作方法如下:
$(".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]
所在的位置.
prev
method will search for the previous element, which is where your input[hidden]
is.
及其href
而不是hre
,位于<a/>
标记中.
And its href
, not hre
, in the <a/>
tags.