我的列表页面中有这样的代码:
<g:each in="${clientTripInstanceList}" status="i" var="clientTripInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}" id="${(i % 2) == 0 ? 'odd' : 'even'}">
<td onclick="show_view('${clientTripInstance.id}')" class = "td_link">${fieldValue(bean: clientTripInstance, field: "number")}</td>
<td onclick="show_view('${clientTripInstance.id}')" class = "td_link">${fieldValue(bean: clientTripInstance, field: "client")}</td>
</tr>
</g:each>
现在
function show_view(trip_id){
var rowId = // get the id of the row
}
从这个函数中,我需要获取上面指定的表行的 id。我如何使用 jQuery 获得它?我试过这个:
var rowId = (this).attr("id");
这根本不起作用。请注意我在 <tr>
中定义了 id不在<td>
中我在其中调用该函数,并且该函数在其他页面中定义。
最佳答案
您忘记了美元符号:(
var rowId = $(this).attr("id");
编辑
<g:each in="${clientTripInstanceList}" status="i" var="clientTripInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}" id="${(i % 2) == 0 ? 'odd' : 'even'}">
<td id="${clientTripInstance.id}" onclick="show_view('${clientTripInstance.id}')" class= "td_link">${fieldValue(bean: clientTripInstance, field: "number")}</td>
<td onclick="show_view('${clientTripInstance.id}')" class= "td_link">${fieldValue(bean: clientTripInstance, field: "client")}</td>
</tr>
</g:each>
function show_view(trip_id){
var $this = $('#' + trip_id);
var row_id = $this.parent().attr('id');
}
关于javascript - 如何使用 jQuery 获取传递的 html 元素的 Id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6382218/