首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jquery数据可忽略参考错误

Jquery数据可忽略参考错误
EN

Stack Overflow用户
提问于 2017-10-08 13:16:52
回答 2查看 2.6K关注 0票数 0

我已经把它放在变量中了,但是它仍然给了我一个错误,它说没有定义未定义的引用错误oTable。

代码语言:javascript
复制
<script type="text/javascript">
$(document).ready(function(){
    $("#test").click(function(){
         $.ajax({
          url: "https://jsonplaceholder.typicode.com/posts",
          success: function( result ) {
          var oTable = $("#datatable").DataTable({
                processing: true,
                data: result,
                columns: [
                    {data: 'id'},
                    {data: 'title'}
                ]
            });
          }
        });
    });
    $("#reload").click(function(){
        oTable.DataTable().ajax.reload();
    });

});
</script>

这是我的html

代码语言:javascript
复制
<table id="datatable">
<thead>
  <tr>
    <th>ID</th>
    <th>TITLE</th> 
  </tr>
</thead>
</table>

请帮我谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-08 14:07:50

希望这对你有用

使用Datatable方法加载ajax,

无需重新启动重新加载数据,使用oTable.ajax.reload()

我将oTable更改为全局变量

代码语言:javascript
复制
  $(document).ready(function () {

            $("#reload").click(function () {
                oTable.ajax.reload();
            });
            $("#test").click(function () {
                window.oTable = $('#datatable').DataTable({
                    "ajax": {
                        "url": "https://jsonplaceholder.typicode.com/posts",
                        "dataSrc": ""
                    },
                    "columns": [{
                            "data": "id"
                        },
                        {
                            "data": "title"
                        },

                    ]
                });
            });
        });
代码语言:javascript
复制
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">

    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script type='text/javascript'>
       
      
    </script>

</head>

<body>

    <button id="reload">
    reaload 
    </button>
    <button id="test">
    test 
    </button>

    <div class="container">
        <table id="datatable">
            <tr>
                <th>ID</th>
                <th>TITLE</th>
            </tr>

            </thead>


        </table>
    </div>

票数 0
EN

Stack Overflow用户

发布于 2017-10-08 13:23:22

您的oTable变量定义是在onSuccess回调上,因此它不能从onSuccess回调范围之外调用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46631521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档