javascript - 如何使用 jQuery 获取传递的 html 元素的 Id?

标签 javascript jquery grails groovy

我的列表页面中有这样的代码:

 <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/

相关文章:

spring - Grails-Spring Security插件-添加 'ROLE_NO_ROLES'后用户拥有 'ROLE_ADMIN'

azure - 用于DocumentDB的Grails插件

javascript - Ajax Get 方法不使用 Ember Js 返回值

使用 Jquery 插入 LI 元素时,JavaScript 有时不会调整 UL 元素的高度

当有多个单选按钮值时,jquery 单选按钮值不起作用

php - SyntaxError : JSON. parse : unexpected character at line 1 column 2 of the JSON data -- FireBug reports this error. 任何解决方案?

javascript - 基于offset()创建onClick jquery工具提示。当用户到达底部时,一切都运行良好,工具提示实际上会隐藏

javascript - 检查数组是否具有另一个数组的键值对

javascript - 将 Nashorn 迁移到 GraalVM

spring - 如何通过 spring-security-ui 保存属于用户的合作伙伴?