Javascript jQuery从列中的隐藏输入获取值
我有下面的HTML表格Javascript jQuery从列中的隐藏输入获取值,javascript,jquery,html,Javascript,Jquery,Html,我有下面的HTML表格 <table> <thead> <tr> <th>Nr.</th> <th>Name</th> <th>Info</th> </tr> </thead> <tbody> <tr> <td>1</td>
<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>
以下是您的操作方法:
$(".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
方法将搜索上一个元素,这是您的输入[隐藏]
所在的位置
它的
href
,而不是hre
,位于
标签中。以下是您的操作方法:
$(".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
方法将搜索上一个元素,这是您的输入[隐藏]
所在的位置
并且它的
href
,而不是hre
,在
标记中。您也可以使用属性
不需要使用隐藏字段
$(".info").click(function(e) {
e.preventDefault();
alert($(this).data('hidden')); // or $(this).attr('data-hidden');
});
您还可以使用属性
不需要使用隐藏字段
$(".info").click(function(e) {
e.preventDefault();
alert($(this).data('hidden')); // or $(this).attr('data-hidden');
});
不要忘记
返回false代码>或e.preventDefault()
.Hmm..刚刚添加了..我不介意,因为href
指向#
@FrédéricHamidi:An#
的href
不会神奇地停止默认操作<代码>#
用于跳转到页面中的不同锚#
将跳转到页面顶部并将#
添加到URL,除非您停止默认操作。@hungerpain:阅读我以前的评论#
不会神奇地停止默认操作。@FrédéricHamidi:我知道这一点是因为我最近忘记了这一点,并且想知道为什么我的页面一直跳到顶部。别忘了返回false代码>或e.preventDefault()
.Hmm..刚刚添加了..我不介意,因为href
指向#
@FrédéricHamidi:An#
的href
不会神奇地停止默认操作<代码>#
用于跳转到页面中的不同锚#
将跳转到页面顶部并将#
添加到URL,除非您停止默认操作。@hungerpain:阅读我以前的评论#
不会神奇地停止默认操作。@FrédéricHamidi:我知道这一点是因为我最近忘记了这一点,并且想知道为什么我的页面一直跳到顶部。