我有一个具有contenteditable值的表,当我更改值时,ajax动作将更新DB的值。
问题是在警报继续出现后,单击框然后单击另一个框,然后又不能执行其他任何操作。
有没有一种方法可以防止其他点击功能一旦启动?
代码JS:
function showEdit(editableObj) {
$(editableObj).css("background", "#FFF");
}
function saveToDatabase(editableObj, column, id) {
var isGood = confirm('Are you sure?');
if (isGood) {
$(editableObj).css("background", "#FFF url(./img/loaderIcon.gif) no-repeat right");
$.ajax({
url: "saveedit.php",
type: "POST",
data: 'column=' + column + '&editval=' + editableObj.innerHTML + '&id=' + id,
success: function(data) {
$(editableObj).css("background", "#FDFDFD");
}
});
// }
} else {
alert('Abort');
}
}
php表:
echo '<td contenteditable="true" onBlur="saveToDatabase(this, \'nameDB\', \''.$res2['valuefromDB'].'\')" onClick="showEdit(this);">' .$res2['valuefromDB'] .'</td>';
最佳答案
您可以使用标志来阻止激活:
var saveEnabled = true; // Only let you save when this is true
function saveToDatabase(editableObj, column, id) {
if (!saveEnabled) return; // If saveEnabled is false do not continue
saveEnabled = false;
var isGood = confirm('Are you sure?');
if (isGood) {
$(editableObj).css("background", "#FFF url(./img/loaderIcon.gif) no-repeat right");
$.ajax({
url: "saveedit.php",
type: "POST",
data: 'column=' + column + '&editval=' + editableObj.innerHTML + '&id=' + id,
complete: function(data) { // Errors should be handled before this
$(editableObj).css("background", "#FDFDFD");
saveEnabled = true;
}
});
// }
} else {
alert('Abort');
saveEnabled = true;
}
}
关于javascript - 防止实时表上传时出现双重激活功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60264243/