Javascript 如何在ajax之外访问dataTable变量
我有一个在函数调用中创建的数据表。但是如果我想基于创建新行,它会给出一个错误,即表变量未定义,这是可以理解的,因为它位于函数调用内部,而不是全局的。那么,我如何为此创建一个解决方案,并在Javascript 如何在ajax之外访问dataTable变量,javascript,jquery,ajax,datatables,Javascript,Jquery,Ajax,Datatables,我有一个在函数调用中创建的数据表。但是如果我想基于创建新行,它会给出一个错误,即表变量未定义,这是可以理解的,因为它位于函数调用内部,而不是全局的。那么,我如何为此创建一个解决方案,并在$(document).ready()部分下添加事件侦听器呢 我的JS文件的结构非常简陋,但它的用意就是这样 $(document).ready(function() { var table=null; $('#button').click(function() {
$(document).ready()部分下添加事件侦听器呢
我的JS文件的结构非常简陋,但它的用意就是这样
$(document).ready(function()
{
var table=null;
$('#button').click(function()
{
callfunction1();
}
callfunction1()
{
$.ajax({
'success': function(response) //response contains the plain table
{
createDatatable(response)
}
})
}
createDatatable(response)
{
$('#division').html(response); //creating the plain table
table=$('#tableId').Datatable({}); //converting it to datatable
}
//I want to add event listeners here, because if I add
//it anywhere else, it doesn't work, because basically
//it's a function call.
}
您可以在更大范围内创建表变量的实例,例如:
//the table is now a window object variable window.table
var table = null;
$(document).ready(function()
{
$('#button').click(function()
{
callfunction1();
}
callfunction1()
{
$.ajax({
'success': createDatatable()
})
}
createDatatable()
{
table=$('#tableId').Datatable({})
}
//the table is binded in the window scope so you can use in your event listeners
}
您可以在更大范围内创建表变量的实例,例如:
//the table is now a window object variable window.table
var table = null;
$(document).ready(function()
{
$('#button').click(function()
{
callfunction1();
}
callfunction1()
{
$.ajax({
'success': createDatatable()
})
}
createDatatable()
{
table=$('#tableId').Datatable({})
}
//the table is binded in the window scope so you can use in your event listeners
}
如果您选择一个var表
声明并删除另一个,则以下操作应该有效
var table; //accessible everywhere
$(document).ready(function()
{
var table; //accessible anywhere in this function
$('#button').click(function() {
callfunction1();
}); //); were missing
function callfunction1 ()
{
$.ajax({
'success': createDatatable //no () here, you want to pass a function, not the result of a function call
});
}
function createDatatable()
{
table=$('#tableId').Datatable({});
}
}
这应该不会给出任何错误,但我不确定这是否是您想要做的。如果您选择一个var表
声明并删除另一个,则以下操作应该有效
var table; //accessible everywhere
$(document).ready(function()
{
var table; //accessible anywhere in this function
$('#button').click(function() {
callfunction1();
}); //); were missing
function callfunction1 ()
{
$.ajax({
'success': createDatatable //no () here, you want to pass a function, not the result of a function call
});
}
function createDatatable()
{
table=$('#tableId').Datatable({});
}
}
这应该不会有错误,但我不确定这是否是您想要做的。所以在您的所有答案(我非常感谢)之后,我找到了一种不同的方法,我添加了一个$.ajax({}}\uuu.done()
(这与在ajax调用之外访问dataTable变量一样好)函数来托管我的事件侦听器以访问我的dataTable
再次感谢大家的回答。
编辑:根据正确解决方案的要求
$(document).ready(function()
{
var table=null;
$('#button').click(function()
{
callfunction1();
}
callfunction1()
{
$.ajax({
'success': function(response) //response contains the plain table
{
createDatatable(response)
}
}).done(function()
{
//add event listener here,You can access the table variable here. but you can not access the variable outside, instead just pass the variable to another function.
console.log(table);//this will work.
});
}
createDatatable(response)
{
$('#division').html(response); //creating the plain table
table=$('#tableId').Datatable({}); //converting it to datatable
}
}
因此,在您给出了所有答案(对此我非常感谢)之后,我找到了一种不同的方法,我添加了一个$.ajax({}}uu.done()
(这相当于在ajax调用之外访问dataTable变量)函数来托管我的事件侦听器以访问我的dataTable
再次感谢大家的回答。
编辑:根据正确解决方案的要求
$(document).ready(function()
{
var table=null;
$('#button').click(function()
{
callfunction1();
}
callfunction1()
{
$.ajax({
'success': function(response) //response contains the plain table
{
createDatatable(response)
}
}).done(function()
{
//add event listener here,You can access the table variable here. but you can not access the variable outside, instead just pass the variable to another function.
console.log(table);//this will work.
});
}
createDatatable(response)
{
$('#division').html(response); //creating the plain table
table=$('#tableId').Datatable({}); //converting it to datatable
}
}
console.log(table)[在本例中为null]将在createDatatable()函数调用(AJAX)之前执行。因此这不会起作用。您对console.log的看法是正确的,我删除了该行。如果console.log(table)将为null,则访问document.ready()中的表也将返回null,这是我得到的。console.log(table)[在本例中为null]将在createDatatable()函数调用(AJAX)之前执行。因此这不起作用。关于console.log
,您是对的,我删除了该行。如果console.log(table)将为null,则访问document.ready()中的表也将返回null,这就是我得到的。现在,document.ready中的表变量不起作用,并显示“undefined”因为ajax调用。为了更好地理解ajax调用,我正在编辑这个问题。var table;
之后,表仍然没有定义。只有在调用createDatatable时才会分配它,这只发生在单击按钮时。如果要立即创建表,只需将table=$('#tableId')。Datatable({})
在ready函数的开头,我看到编辑时唯一的问题是您写了两次var table
king说“未定义”是因为ajax调用。我编辑这个问题是为了更好地理解ajax调用。var table;
,table仍然未定义。它只有在调用createDatatable时才被分配,而调用createDatatable只需单击一个按钮。如果您想立即创建表,那么只需将table=$(“#tableId”)放在.Datatable({});
在ready函数的开头,我看到编辑的唯一问题是编写var table
两次。在createTable函数中,您必须忽略var,只需编写table=…
我已更改为justtable=
,但仍然相同请添加代码(解决方案的一个示例)并将您的答案标记为已接受。也许它将在将来帮助其他人。请添加代码(解决方案的示例)并将您的答案标记为已接受。也许它将在将来帮助其他人。