Javascript jQuery 获取表格行中的隐藏字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10817041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery to get Hidden Field Value in Table Row
提问by LCJ
I have a table with a hidden field in each row. I need to Alert the value of hidden field when a button in that row is clicked. I have the following jQuery code. But it does not work. How do we make it work?
我有一个表,每行都有一个隐藏字段。单击该行中的按钮时,我需要提醒隐藏字段的值。我有以下 jQuery 代码。但它不起作用。我们如何让它发挥作用?
CODE: http://jsfiddle.net/Lijo/xWanB/
代码:http: //jsfiddle.net/Lijo/xWanB/
<script>
$(document).ready(function () {
//"Show ID" for Associate Button Click
$('.resultGridTable tr > td > .actionButtonNational').click(function () {
//"this" means show ID button
//Traversing to get the parent row and then the required columns in the row
var associateID = $(this).parents('tr:first > .wrapperDivHidden input[type=hidden]').val();
alert(associateID);
return false;
});
});
</script>
HTML
HTML
<td>
XXXXX
<input type="submit" name="ctl00$detailContentPlaceholder$grdSubscribedAssociates$ctl04$btnNational"
value="Show ID" id="detailContentPlaceholder_grdSubscribedAssociates_btnNational_2"
class="actionButtonNational" style="color: White; background-color: #A7A7A6;
font-weight: bold; width: 60px" />
<div id="wrapperDivHidden" class="wrapperDivHidden">
<input type="hidden" name="ctl00$detailContentPlaceholder$grdSubscribedAssociates$ctl04$hdnAssociateID"
id="detailContentPlaceholder_grdSubscribedAssociates_hdnAssociateID_2"value="789345680" />
</div>
</td>
回答by Fabrizio Calderan
your selector starts with tr:first > .wrapperDivHidden ...
but .wrapperDivHidden
is not an immediate child of tr
so change your selector like so:
您的选择器以tr:first > .wrapperDivHidden ...
但.wrapperDivHidden
不是直接子代开始,tr
因此请像这样更改您的选择器:
$(this).parents('tr').find('.wrapperDivHidden input[type="hidden"]').val();
Fiddle: http://jsfiddle.net/xWanB/3/
回答by Kapil Khandelwal
Try this:
尝试这个:
<script type="text/javascript">
$(document).ready(function () {
//"Show ID" for Associate Button Click
$('.actionButtonNational').click(function () {
var associateID = $('input[type=hidden]', $(this).closest("td")).val();
alert(associateID);
return false;
});
});
</script>
回答by Lowkase
Here is an over simplified example of what you are trying to do:
这是您尝试执行的操作的过度简化示例:
回答by ahmed shaji
if the first column of your row is hidden then use this var x = $('input[type=hidden]', $(this).find("td:first")).val();
如果您的行的第一列被隐藏,则使用此 var x = $('input[type=hidden]', $(this).find("td:first")).val();