【问题标题】:jQuery Datatable add CSS to cell after update Part 2jQuery Datatable 更新后将 CSS 添加到单元格第 2 部分
【发布时间】:2018-01-22 15:42:55
【问题描述】:

这是a previous question的后续行动。

我需要做的是仅在进程成功更新记录后为单元格着色。

$('#example1').on('blur', 'tr > td > .editForecast', function(e) 
{
  e.preventDefault();
  var forecastnewval = $(this).val();
  var processData = '';

  if(this.value !== $(this).attr('value'))
  {
    $.post('api/inlineEditProcess.php', {forecastnewval:forecastnewval}, function(data)
    {
     processData = data; // this is what I'm trying to get now
     console.log('this is inside ' + processData); // this prints processData
    }

    console.log('this is outside ' + processData); // this does not print processData

    $(this).css('background-color', '#99FF66'); // this changes the background color of the cell
  }
  else
  {
    console.log('nothing changed');
  }
});

如果您在上面的代码中注意到,我正在尝试从 $.POST 中检索变量 processData 并在另一个 IF/ELSE 语句中使用它。

变量 processData 将是一个读取“成功”或“失败”的字符串。这显然来自运行 UPDATE 查询的 PHP 进程脚本,然后返回指定的单词。

我想在 IF/ELSE 中使用 processData,它将单元格的背景颜色更改为绿色(成功)或红色(失败)。

我只是想检索从 $.POST 中创建的变量。

我可以这样做吗?如果可以,怎么做?

【问题讨论】:

  • 如果您在 post 函数之外声明变量 processData,它应该可以工作。你现在可能要打印 undefined 因为你在函数的范围内声明了变量

标签: javascript jquery css datatables


【解决方案1】:

您的变量 processData 包含在一个函数中,因此在函数外部计算时将返回 null

$('#example1').on('blur', 'tr > td > .editForecast', function(e) {
  e.preventDefault();
  var forecastnewval = $(this).val();
  var processData = "";

  if (this.value !== $(this).attr('value')) {
    $.post('api/inlineEditProcess.php', {
      forecastnewval: forecastnewval
    }, function(data) {
      processData = data; // this is what I'm trying to get now
    });

    console.log(processData); // this does not print processData

    $(this).css('background-color', '#99FF66'); // this changes the background color of the cell
  } else {
    console.log('nothing changed');
  }
});

【讨论】:

  • 我更新了我的代码。请查看 console.logs - 我只能从 Post 内部打印 processData。我无法在帖子之外访问它。想法?
  • 是的,post 函数没有正确关闭。在我的回答中进行了修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多