Javascript返回我对象Object

詹姆斯·D

我有以下代码可使用datatables.net插件创建数据表:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script type='text/javascript' src='//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js'></script>

        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
        <link type="text/css" href="https://cdn.datatables.net/1.10.3/css/jquery.dataTables.css" />
        <link type="text/css" href="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.css" />

        <script src="https://cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/plug-ins/380cb78f450/integration/bootstrap/3/dataTables.bootstrap.js"></script>

        <script type='text/javascript' src="https://datatables.net/release-datatables/extensions/KeyTable/js/dataTables.keyTable.js"></script>

    </head>
    <body>
        <script type="text/javascript">
            //   function day2Date( day, year ) {
            // return new Date(year,0,day);
            //}
            $(document).ready(function() {

                $('#example').dataTable({
                    "ajax": "table1.php",
                    "columns": [{
                            "data": "ID"
                        }, {
                            "data": "naziv"
                        }, {
                            "data": "vrsta"
                        },

                    ],
                    "columnDefs": [{
                        "targets": 2,
                        "data": "download_link",
                        "render": function(data, type, full, meta) {
                            // return data; 
                            return '<button class="btn btn-success">' + data + '</button>';
                        }
                    }]
                });
            });
            var table = $('#example').DataTable();
            $(document).ready(function() {
                $('#example tbody').on('click', 'td', function() {
                    console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
                    // alert('Row:'+$(this).parent().find('td').html().trim());
                    //alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());

                });
                $('#example tbody').on('click', 'tr', function() {
                    console.log('Row index: ', table.row(this).index());
                });
            });
        </script>
        <div class="container">
            <table id="example" class="table table-striped table-bordered table-responsitive" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Naziv</th>
                        <th>Vrsta</th>

                    </tr>
                </thead>

                <tfoot>
                    <tr>
                        <th>ID</th>
                        <th>Naziv</th>
                        <th>Vrsta</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </body>
</html>

我需要获取行索引,因此我可以从上面的代码中看到:

$('#example tbody').on( 'click', 'tr', function () {
    console.log( 'Row index: '+table.row( this ).index() );

就像我在datatables网站上的文档中看到的那样,但是此代码仅返回我 [object Object]

例子:

Data:12Row:2Column:Naziv 
Row index: [object Object] 

为什么?有人有解释吗?

编码消失了

您已经任何DOM ready处理程序之外但在发生该事件的元素之前包含了一行关键代码这意味着$('#example')不返回匹配项:

将此行放在DOM ready处理程序中:

var table = $('#example').DataTable();

例如

$(document).ready(function () {
    var table = $('#example').DataTable();
    $('#example tbody').on('click', 'td', function () {
        console.log('Data:' + $(this).html().trim() + 'Row:' + $(this).parent().find('td').html().trim() + 'Column:' + $('#example thead tr th').eq($(this).index()).html().trim());
        // alert('Row:'+$(this).parent().find('td').html().trim());
        //alert('Column:'+$('#example thead tr th').eq($(this).index()).html().trim());

    });
    $('#example tbody').on('click', 'tr', function () {
        console.log('Row index: ', table.row(this).index());
    });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

返回对象的javascript函数返回[object Object]

Javascript 对象值返回为 [object,object]

为什么ObjectOutputStream.readObject()返回Object类型的对象,而不返回我写入它的类型的对象?

从ajax文件返回PHP对象到我的JavaScript代码

我如何在JavaScript中返回两个对象

如何使用 Object.keys Javascript 返回对象

JSON返回Object对象

JavaScript 返回 [object Object]

我的Hashmap对象值列表对象仅返回我放置在ArrayList <Map <String,Object >>>();中的最后一个对象;

javascript [Object] 在我的对象中而不是真实数据

为什么我的HTML构造函数对象返回[object Object]而不是[HTMLElementElement]?

javascript关闭并返回对象

我无法在.then()中返回对象

从我的对象返回静态数组

JQuery Ajax 返回 [object 对象]

JavaScript-返回[object Object]

即使我从我的 javascript 函数返回一个数组,我也得到一个对象

返回object或object及其父对象

带有方法的Javascript对象返回“没有方法'我的方法名称'”错误

我的javascript异步等待API请求返回了一个奇怪的对象

我想比较对象的 JSON 数组中的内部数组,并根据条件使用 JavaScript 返回新数组

JavaScript对象的值返回null

Java:从ScriptEngine JavaScript返回对象

JavaScript对象的函数未返回

Javascript对象不返回变量

JavaScript LocaleDateString返回日期对象

总是返回false | Javascript | 对象

foreach返回对象属性javascript

传递密钥并返回对象javascript