javascript - JQuery DataTables 如何在使用分页时从表中获取选定的行?

标签 javascript jquery datatables

例如,我从第二页选择(选中)2 行,而不是转到第一页并选择 3 行。当我停留在第一页时,我想从选定的 5 行中获取信息。

$('tr.row_selected') - 不工作

谢谢。

更新。

我创建了这样的处理程序:

$('#example').find('tr td.sel-checkbox').live("点击", function () { /*代码在这里*/ });

但是现在当点击事件被处理时,表中的行被隐藏了。我觉得可能是DataTables的排序或者分组操作。知道我必须用它做什么吗?

最佳答案

选中复选框后,将所需的行信息作为键值对存储在全局对象中

我不记得具体是怎么做到的,但语法是这样的

$('input[type=checkbox]').click(function()
{
    var row = $(this).parent(); //this or something like it, you want the TR element, it's just a matter of how far up you need to go
    var columns = row.children(); //these are the td elements

    var id = columns[0].val(); //since these are TDs, you may need to go down another element to get to the actual value

    if (!this.checked) //becomes checked (not sure may be the other way around, don't remember when this event will get fired)
    {
        var val1 = columns[1].val();
        var val2 = columns[2].val();

        myCheckValues[id] =[val1,val2]; //Add the data to your global object which should be declared on document ready
    }
    else delete myCheckValues[id];
});

提交时,从对象中获取选定的行:

for (var i = 0; i < myCheckValues.length; i++)
...

抱歉,很长时间没用过 JS,所以按原样编写代码可能行不通,但你明白了。

关于javascript - JQuery DataTables 如何在使用分页时从表中获取选定的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321353/

相关文章:

javascript - 如何将对象迭代到对象数组

javascript - 根据影响 div 中所有类的类更改 $(this) 输入字段颜色

javascript - jQuery(附加、分离、重新附加)事件处理程序问题

php - 数据表 |如何做自己的选择?

javascript - 如何处理 <Empty Div>

javascript - 数据表更改导出格式

javascript - 在 D3 中补间转换后更新 textPath

javascript - HTML 元素的 value 属性给出了未定义的值

javascript - CSS Transform Skew 和 Scale 在 Microsoft Edge 中无法正常工作

jquery - 使用自定义宽高,jQuery Mobile Dialog center to screen