确认对话框未显示
confirmation dialog isn't showing
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: "POST",
data: {
id: <?php echo $row['serial']?>,
type: "succeed"
},
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=="3"){
$.ajax({
url: "update.php",
type: "POST",
data: {
id: <?php echo $row['serial']?>,
type: "canceled"
},
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{
console.log("Update canceled");
}
}
});
我在用户更改时添加了一个确认对话框 option
。当我 运行 代码并更改 option
时,我没有收到任何确认对话框,但是,当我在没有确认对话框的情况下工作时,一切正常。为什么会这样?我在控制台中遇到错误 Uncaught TypeError: confirm is not a function at HTMLSelectElement.<anonymous>
I was looking at w3schools courses but, i am unable to understand anything
您应该避免使用 HTML 和 Window 对象和属性的名称:
https://www.w3schools.com/js/js_reserved.asp
don't use confirm use ConfirmDialog or something!
您可以使用 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) {
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: "POST",
data: {
id: <?php echo $row['serial']?>,
type: "succeed"
},
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=="3"){
$.ajax({
url: "update.php",
type: "POST",
data: {
id: <?php echo $row['serial']?>,
type: "canceled"
},
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{
console.log("Update canceled");
}
}
});
我在用户更改时添加了一个确认对话框 option
。当我 运行 代码并更改 option
时,我没有收到任何确认对话框,但是,当我在没有确认对话框的情况下工作时,一切正常。为什么会这样?我在控制台中遇到错误 Uncaught TypeError: confirm is not a function at HTMLSelectElement.<anonymous>
I was looking at w3schools courses but, i am unable to understand anything
您应该避免使用 HTML 和 Window 对象和属性的名称:
https://www.w3schools.com/js/js_reserved.asp
don't use confirm use ConfirmDialog or something!
您可以使用 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) {