我尝试使用具有以下配置的 SweetAlert 2.1.2:
swal({
title: "My Event Title",
text: "My event message",
icon: "warning",
html: true,
closeOnClickOutside: true,
content: {
element: "input",
attributes: {
placeholder: "User text",
type: "input"
}
},
buttons: {
cancelMove: {
text: "Cancel",
value: "c"
},
sendConfirm: {
text: "Yes, continue",
value: "s"
},
}
},
function(value) {
switch (value) {
case "cancelMove":
swal("Move cancelled");
break;
case "sendConfirm":
swal("Sending confirmation ...");
break;
default:
swal("Good bye!!!");
}
});
我认为我正确地遵循了他们的配置步骤。但是,显然我不是。
最佳答案
在使用其定义的值单击按钮期间,您将传入此按钮值的 value
。我的意思是 cancelMove
发送 value = c
和 sendConfirm
发送 value = s
。
还可以使用 .then(
而不是 }, function(
.
下面的工作片段:
swal({
title: "My Event Title",
text: "My event message",
icon: "warning",
html: true,
closeOnClickOutside: true,
content: {
element: "input",
attributes: {
placeholder: "User text",
type: "input"
}
},
buttons: {
cancelMove: {
text: "Cancel",
value: "c"
},
sendConfirm: {
text: "Yes, continue",
value: "s"
},
}
}).then((value)=>{
switch (value) {
case "c":
swal("Move cancelled");
break;
case "s":
swal("Sending confirmation ...");
break;
default:
swal("Good bye!!!");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
关于javascript - SweetAlert 配置困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59041098/