我在文档中定义了一个数据表,如下所示
$(document).ready(function() {
var oTable = $('#systemgoals').dataTable({});
我有一个带有表单的对话框和一个带有以下功能的按钮
buttons: {
"Add System Goal": function() {
var formfilled = true;
$("form#add_systemgoal :text, form#add_systemgoal :file, form#add_systemgoal :checkbox, form#add_systemgoal select, form#add_systemgoal textarea").each(function() {
if($(this).val() === "")
formfilled = false;
});
if(formfilled === true){
$('form#add_systemgoal .error').remove();
var formdata = $('form#add_systemgoal').serialize();
$.ajaxSetup({async: false});
$.ajax({
type: "POST",
url: '/admin/systemgoals/systemgoalupdate?format=html',
data: formdata,
success: function (data) {
var obj = $.parseJSON(data);
if(obj.success){
$(oTable).dataTable().fnAddData( [
obj.inserted.goal_id,
obj.inserted.value,
obj.inserted.status,
obj.inserted.points_per_action,
obj.inserted.created,
obj.inserted.due,
obj.inserted.expires ]);
}
}
});
}
ajax很好,表单发布正确的值响应,但fnAddData返回错误
ReferenceError:oTable未定义
任何建议表示赞赏
谢谢
答案 0 :(得分:0)
您没有将oTable设置为全局变量,为什么没有定义对话框上的oTable,如果您想在脚本上定义它,请执行以下操作:
var oTable;
$(document).ready(function() {
oTable = $('#systemgoals').dataTable({});
在你的对话框上
oTable.fnAddData( [
obj.inserted.goal_id,
obj.inserted.value,
obj.inserted.status,
obj.inserted.points_per_action,
obj.inserted.created,
obj.inserted.due,
obj.inserted.expires ]);
}
或只是简单地做
var oTable = $('#systemgoals').dataTable().fnAddData...
最好的问候