jQuery 返回空字符串作为 ID 表示

jQuery returning empty string as ID for This

本文关键字:ID 表示 字符串 返回 jQuery      更新时间:2023-09-26

使用服务器端表时,我正在尝试构建一个已选择的行数组,同时在每个行中切换类 .active。

"this"似乎是正确的,因为它返回已单击的选择器。 但是,this.id 始终为空字符串 (")。 我做错了什么?

var active = [];
$('#adminstable tbody').on('click', 'tr', function () {
    var id = this.id;
    var index = $.inArray(id, active);
    if ( index === -1 ) {
        active.push( id );
    } else {
        active.splice( index, 1 );
    }
    $(this).toggleClass('active');
} );

this指的是<tr>,它没有ID,是吗? 如果没有,这似乎是问题所在。

尝试:

var id = $(this).parents('table')[0].id;

由于这是数据表,我只需要添加

DT_RowId:"Row_" + item.id.to_s

到响应哈希,它插入了我需要的 ID。