开发者

How to get the Id of Passed html element Using jQuery?

开发者 https://www.devze.com 2023-03-13 19:38 出处:网络
I have code in my list page something like this: <g:each in=\"${clientTripInstanceList}\" status=\"i\" var=\"clientTripInstance\">

I have code in my list page something like this:

 <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>开发者_如何学JAVA;

now

function show_view(trip_id){
   var rowId =           // get the id of the row

 }

from this function I need to get the id of the table row which is specified above. How do I get that using jQuery? I tried this:

var rowId = (this).attr("id"); 

which is not working at all. Note the point that I have defined id in <tr> not in <td> where I'm calling the function, and the function is defined in other page.


You had forgotten the dollar sign :(

var rowId = $(this).attr("id");

EDIT

<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');
}


To refer the table cell you click in show_view(), you can pass a 'this' parameter to the function, try this:

<td onclick="show_view(this, '${clientTripInstance.id}')" class = "td_link">${fieldValue(bean: clientTripInstance, field: "number")}</td>

function show_view(dom, trip_id) {
    var rowId = $(dom).parent().attr("id");
}


You can also use:

var rowId = this.id

This is just javascript, not jQuery

0

精彩评论

暂无评论...
验证码 换一张
取 消