2013-04-22 56 views
0

我有一个包含每行中下拉列表的表格。行没有id属性。所以,因为行没有id属性,我怎么能得到下拉列表中选定的项目和相应的ID列的值。例如,如果我从第一行中选择一个项目我想要的项目和ID列的值的值,即204使用jQuery选择表格行的值

table to be manipulated using jQuery

这是上表中的HTML代码

<table class="table-1 gapmb40"> 
    <thead> 
     <tr> 
      <th> 
       Status 
      </th> 
      <th> 
       <a class="sortable" href="">Featured</a> 
      </th> 
      <th> 
      </th> 
      <th> 
       <a class="sortable" href="">Date Modified</a> 
      </th> 
      <th> 
      </th> 
      <th> 
       ID 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td> 
       <select class="input-2" name="2"> 
        <option value="New">New</option> 
        <option selected="selected" value="Live">Live</option> 
        <option value="AccountOnly">AccountOnly</option> 
        <option value="Hide">Hide</option> 
        <option value="Suspended">Suspended</option> 
       </select> 
      </td> 
      <td> 
       <a href="">Feature</a> 
      </td> 
      <td> 
       <a href="">View</a> 
      </td> 
      <td> 
       07/03/2013 
      </td> 
      <td style="display: none"> 
       <a href="">LogOnAs</a> 
      </td> 
      <td> 
       204 
      </td> 
     </tr> 
    </tbody> 
</table> 
+5

显示您的HTML。 – 2013-04-22 12:56:59

回答

2

给你所有的选择框一个类...说selectClass并使用jquery类选择器。

试试这个

$('.selectClass').change(function(e){ 
    alert($(this).val()); //gives you the selected value 
    alert($(this).parents('tr').find('td:eq(5)').text()); //gives you the related TD which is 4th column and gets its text 

    //or 
    alert($(this).closest('tr').find('td:eq(5)').text()); 
}); 

fiddle here

+0

回答这个,即使你的问题是最有可能关闭...请张贴您的相关代码也与您的问题..以便其他人可以帮助您... ... – bipen 2013-04-22 13:08:29

+1

bipen,alert($(this).parent()。 。找到( 'TD:EQ(3)')文本());正在给我留下空白值。 – 2013-04-22 13:20:32

+0

@kaustubhshukla更新了试试看......更新提琴太..看看 – bipen 2013-04-22 13:34:46

0

如果你有表ID,那么你可以试试这个..

$("#tbltable tr").click(function() { 
    var selectedText = $(this).find('select :selected').text(); 
    var ColumnID = this.cells[4].innerHTML.toString(); 
    alert(selectedText + " , " + ColumnID); 
}); 
0
<select onchange=sel_change(this.value,'<%=id%>');></select> 

所以在javascript函数就可以得到选择项目值和该行的ID

<script> 

    function sel_change(item,id){ 
     alert("selected item "+item+"from the id "+id); 
    } 

</script>