获取表中select元素的id

时间:2014-07-28 16:15:16

标签: javascript jquery

我有一个包含多行的3列基本引导表。第3列是一个包含值的选择框。现在,当用户点击表格行时,我想要选择此选择框值的id。到目前为止,我有这个...

JSP

<table class="table table-bordered table-hover spacer-top" id="tableSectionDetails"   name="tableSectionDetails" onclick="selectTableRowData();return false;">

<c:forEach var="subSection" items="${section.subSectionList}">
  <tr>
     <td>${section.sectionName}</td>
     <td>${subSection.subSectionName}</td>

     <td>
         <select name ="caseName" class="form-control">
             <c:forEach var="scase" items="${subSecction.SCaseList}">

                <option value="${scase.id}">${scase.caseName}</option>
             </c:forEach>
         </select>

     </td>
   </tr>
</c:forEach> 

</table>

JS

function selectTableRowData(){

     $('#tableSectionDetails tbody td').dblclick(function(){
         var rowIndex = $(this).parent().index();
         alert(rowIndex);  //shows the correct row index but it alerts the same index for all the rows table has instead of the selected row and just one time

         alert($("select.caseName[rowIndex]").text());  //empty...does not return the correct value and again alerts me multiple times (row times) instead of just one time selected row..


    });
}

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$('#tableSectionDetails tbody td').dblclick(function(){
    var value = $(this).closest("tr").find("select").val();
});