Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery从列中的隐藏输入获取值_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery从列中的隐藏输入获取值

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>

我有下面的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>

以下是您的操作方法:

$(".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:我知道这一点是因为我最近忘记了这一点,并且想知道为什么我的页面一直跳到顶部。