这是 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 中创建的变量。
我可以这样做吗?如果可以,怎么做?
最佳答案
您的变量 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');
}
});
关于javascript - jQuery Datatable 在更新第 2 部分后将 CSS 添加到单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385277/