首页 > 解决方案 > 在 Datatables JQuery 和 Ajax 中,DataTable(...) 不是函数

问题描述

我在我的 ASP.NET MVC 项目中收到以下错误。页面中有对 *.js 文件的引用。我尝试了互联网上几乎所有解决方案来解决这个问题,我能找到。

从页面在浏览器中加载时的源代码来看,似乎顺序正确,加载了一次:

<script src="/Scripts/old/Scripts/jquery-1.10.2.js"></script> <!-- I tried new and old jquery Version -->
<script src="/Scripts/bootstrap.js"></script>
<script src="/scripts/bootbox.js"></script>
<script src="/scripts/datatables/jquery.datatables.js"></script>
<script src="/scripts/datatables/datatables.bootstrap.js"></script>

代码(index.cshtml):

$(document).ready(function() {
  $('#customers').DataTable()({
    ajax: {
      url: '/api/customers',
      dataSrc: ''
    },
    columns: [{
      data: "name",
      render: function(data, type, customer) {
        return "<a href='/customers/edit/" + customer.id + "'>" + customer.name + "</a>";
      }
    }, {
      data: "name"
    }, {
      data: "id",
      render: function(data) {
        return "<button class='btn-link js-delete' data-customer-id=" + data + ">Delete</button>";
      }
    }]
  });

  // More Code
});

来自 Chrome 的开发者控制台的错误:

客户:79 未捕获的类型错误:$(...).DataTable(...) 不是
HTMLDocument 中的函数。(客户:79)

Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174)
在 Function.ready (jquery-1.10.2.js: 447)
在 HTMLDocument.completed (jquery-1.10.2.js:118)

有什么建议么?

好的,解决了。第一条评论是对的。

$('#customers').DataTable({而不是$('#customers').DataTable()({

标签: javascriptjquerydatatables

解决方案


您需要在渲染脚本部分之前导入这些脚本。


推荐阅读