[英]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.