2012-05-30 83 views
3

我有一個表格,每行都有一個隱藏字段。當點擊該行中的按鈕時,我需要提醒隱藏字段的值。我有以下jQuery代碼。但它不起作用。我們如何使其工作?jQuery在表格行中獲取隱藏字段值

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

回答

13

您的選擇與tr:first > .wrapperDivHidden ...啓動,但.wrapperDivHidden不是tr立即孩子這麼改變你的選擇,像這樣:

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

小提琴:http://jsfiddle.net/xWanB/3/

+0

謝謝。很好的解釋.. – Lijo

1

試試這個:

<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();

相關問題