2017-03-11 24 views
0

我使用jstl foreach循環顯示數據列表作爲表。在行的單擊中,行中的相應ID值需要發送到jquery onclick()函數。 表-1在使用jstl顯示的表中傳遞行ID到點擊行上的jquery

<table border="1" width="90%" class="table-hover" id="warehouseValue"> 

    <tr> 
     <th>Warehouse Id</th> 
     <th>Warehouse Location</th> 
     <th>Warehouse City</th> 
     <th>Zone</th> 
    </tr> 

    <c:forEach items="${sessionScope.listWarehouse}" var="description"> 
     <tr>   
      <td> 
       <input type="hidden" id="idValue" value="${description.getWarehouseId()}" /> 
       ${description.getWarehouseId()} 
      </td> 
      <td>${description.getWarehouseLocation()}</td> 
      <td>${description.getWarehouseCity()}</td> 
      <td>${description.getWarehouseDescription().getZone()}</td> 
     </tr> 
    </c:forEach> 
</table> 

jQuery的的onclick()上的任何行僅在第一行的id的點擊功能 -

$(document).ready(function() { 
    $("#warehouseValue").click(function() { 
     var rowId = document.getElementById("idValue").value; 
     alert("new "+rowId); 
    }); 
}); 

這裏 - $ {description.getWarehouseId()}是越來越顯示出來。 我如何使每行的相應Id值發送到onclick()函數

回答

0

你可以得到這樣的。 td有一個子輸入元素。

$("#warehouseValue").on('click','td',function(event) { 
     // td has a child input 
     var value1 = $(event.target.children[0]).val(); 
     var value2 = $(event.target).children("input").val(); 
     console.log(value1+" "+value2); 
}); 
0

試試這個。對我來說評論太長了,但不要把它當作答案。

<tr> 
    <th>Warehouse Id</th> 
    <th>Warehouse Location</th> 
    <th>Warehouse City</th> 
    <th>Zone</th> 
</tr> 

<c:forEach items="${sessionScope.listWarehouse}" var="description"> 
    <tr>   
     <td> 
      <input type="hidden" class="id-input" id="idValue" value="${description.getWarehouseId()}" /> 
      ${description.getWarehouseId()} 
     </td> 
     <td>${description.getWarehouseLocation()}</td> 
     <td>${description.getWarehouseCity()}</td> 
     <td>${description.getWarehouseDescription().getZone()}</td> 
    </tr> 
</c:forEach> 

JQuery的

$(document).ready(function() { 
    $("#warehouseValue").on('click','tr',(function() { 
    var $this = $(this); 
    var rowID = $this.find('.id-input');  
     alert("new " + rowId); 
    }); 
}); 
+0

獲取新的[object Object]作爲輸出而不是Id –

相關問題