jQuery获取表格行中隐藏字段的值

3

我有一张表格,每行都有一个隐藏字段。当点击该行中的某个按钮时,我需要弹出隐藏字段的值。我有以下jQuery代码,但它不起作用。如何让它工作?

代码: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

<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>
4个回答

18

你的选择器以 tr:first > .wrapperDivHidden ... 开始,但是 .wrapperDivHidden 不是 tr 的直接子元素,所以请将您的选择器更改为以下内容:

tr:first .wrapperDivHidden ...
$(this).parents('tr').find('.wrapperDivHidden input[type="hidden"]').val();

Fiddle: http://jsfiddle.net/xWanB/3/


2

试试这个:

<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>

0
如果您的行的第一列被隐藏了,那么请使用以下代码: var x = $('input[type=hidden]', $(this).find("td:first")).val();

0

我正在使用ASP.Net HiddenField。因此它可能对我无效。 - LCJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接