【问题标题】:"JSON.stingify is not a function" DataTables When Making Variable制作变量时的“JSON.stringify 不是函数”DataTables
【发布时间】:2018-05-03 05:12:09
【问题描述】:

我已经尝试了很多东西。

我有一个 ID 为 save 的按钮,我正在尝试将 .selected 行转换为 JSON 格式。

这是我的数据表初始化。

<script>
//    var table;
$(document).ready(function() {
    var table = $('#example').DataTable(
        {
"order": [],
                "processing": true,
             "searching": false,
             "paging": false, 
             "autowidth": true,
             'select': {
 'style': 'multi'
          },
 "columnDefs": [
    { orderable: false, targets: '_all' }
]
   });

如果我把点击功能放在$(document).ready(function()

$(document).on('click','#save',function(e){
e.preventDefault();
var data = table.rows(['.selected']).data().toArray();
var json = JSON.stingify( data );
console.log( JSON.stringify(data) );
var json = JSON.stingify( data );
console.log( (json) );
} );

} );

我的控制台返回正确的格式和JSON.stingify is not a function 错误。

[["My","Selected","Row","Row","Row","your","boat"]]

未捕获的类型错误:JSON.stingify 不是函数

我试图将点击事件放在一个函数中

$('#save').click(recordjourney);

并将recordjourney 函数放在$(document).ready(function() DataTables 初始化之外。

但无法让数据表重新初始化行。

function recordjourney() {
 var data = table.rows(['.selected']).data().toArray();

未捕获的类型错误:table.rows 不是函数

我试过重绘它

      $('#example').DataTable().draw();

我已经尝试重新初始化表格

var table = $('#example');

/

var table = rows.closest('table').dataTable();

并使用 api。

    var tr = $(this).closest('tr');
    var rows = table.DataTable().row( tr );
    
    table.api()
         .row( rows )
         .remove()
         .draw();

都会导致不同但相似的错误,无法找到tablerowstable.rows

为什么我可以 console.log 我的 JSON.stringify 很好,但不能把它放在一个变量中? 如何获取 onclick 事件以将行注册到 JSON 字符串化来自数据表的数据?

【问题讨论】:

  • 这是stringify,不是stingify
  • 哦,对了,那好吧。
  • 只需谷歌搜索就可以解决您的问题。无论如何,感谢发布一个完整的问题。

标签: javascript jquery json datatable datatables


【解决方案1】:

你这里有一个错字,stingify 不是一个函数它的stringify

-帮助

【讨论】:

    【解决方案2】:

    你快到了,只是一个错字

    console.log(JSON.stringify({ a: "b" }));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2014-10-20
      • 2020-02-22
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多