问题描述
document.querySelector('#select').addEventListener("change",function() {
var confirm = confirm("Do you want to update data?");
if (confirm == true) {
if (this.value == "1") {
$.ajax({
url: "update.PHP",type: "POST",data: {
id: <?PHP echo $row['serial']?>,type: "pending"
},cache: false,success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
alert("Successfully updated");
location.replace("../deposit/");
}
else if(dataResult.statusCode==201){
alert("Something went wrong");
}else{
alert("Everything went wrong");
}
}
});
}else if(this.value == "2"){
$.ajax({
url: "update.PHP",type: "succeed"
},success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
alert("Successfully updated");
location.replace("../deposit/");
}
else if(dataResult.statusCode==201){
alert("Something went wrong");
}else{
alert("Everything went wrong");
}
}
});
}else if(this.value=="3"){
$.ajax({
url: "update.PHP",type: "canceled"
},success: function(dataResult){
var dataResult = JSON.parse(dataResult);
if(dataResult.statusCode==200){
alert("Successfully updated");
location.replace("../deposit/");
}
else if(dataResult.statusCode==201){
alert("Something went wrong");
}else{
alert("Everything went wrong");
}
}
});
}else{
console.log("Update canceled");
}
}
});
我在用户更改 option
时添加了一个确认对话框。当我运行代码并更改 option
时,我没有收到任何确认对话框,但是,当我在没有确认对话框的情况下工作时,一切正常。为什么会发生?我在控制台 Uncaught TypeError: confirm is not a function at HTMLSelectElement.<anonymous>
How do I append new data to existing doc/docx file using Python
解决方法
您可以使用 window.confirm
,因为它是一个暴露给 Window 的全局函数。
https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm
您也可以将代码更改为此(您不需要 var confirm
。这也是问题所在,因为您使用 confirm
两次,但用途不同。
if (confirm("Do you want to update data?")) {
如果确实需要变量,请使用布尔值的标准命名约定,即在变量名称前添加“is”、“has”、“can”或“should”:
var hasConfirmed = confirm("Do you want to update data?");
if (hasConfirmed == true) {
,
您应该避免使用 HTML 和 Window 对象和属性的名称:
https://www.w3schools.com/js/js_reserved.asp
不要用confirm,用ConfirmDialog什么的!