我写了下面的代码,我想在“取消”按钮下调用一段代码:
vm.saveGroup = function(){
SweetAlert.swal({
title: "Name this Device Group",
text: "Please provide a name that you want your device group to be saved under. Also, make sure that you already specified all needed filters before you save the list.",
type: "input",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
inputPlaceholder: "Device Group name"
},
function(inputValue){
if (inputValue === false) {
return false;
}
if (inputValue === "") {
/*eslint-disable */
swal.showInputError("You need to write something!");
/*eslint-enable */
return false;
}
deviceGroupsFactory.saveGroup(inputValue, vm.filterOutput)
.then(function(response){
if (response.status == 200){
SweetAlert.swal("Device Group saved!", "You should now see your device group on the list.", "success");
$state.go('main.template.devices.groups');
}
else {
SweetAlert.swal("Error!", response.statusText, "error");
console.log('xxx');
}
});
});
}
但我不能调用“取消点击”。我在文档中寻找但找不到解决方案。
最佳答案
您向swal 构造函数传递了太多参数。它只需要两个:
swal(options, callback)
- 选项:设计警报的属性
- callback:管理事件的回调函数
当您使用简单确认时,回调仅接收一个指示用户选择的参数:
- true:确认
- false:取消
使用输入框,您将接收用户输入的字符串作为参数。
合并inputbox并确认后,你会收到:
- 输入的字符串值:当用户按下确认
- false:当用户按下取消时
所以,您必须使用 Strict Equality Comparison 为了知道用户是否按下了取消键或在输入框中插入了字符串 false
。
请看下面的简单例子:
swal({
title: "Are you sure?",
text: "Press CANCEL, please!",
type: "input",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "CONFIRM",
cancelButtonText: "CANCEL",
closeOnConfirm: false,
closeOnCancel: false
},
function(inputValue){
//Use the "Strict Equality Comparison" to accept the user's input "false" as string)
if (inputValue===false) {
swal("Well done!");
console.log("Do here everything you want");
} else {
swal("Oh no...","press CANCEL please!");
console.log("The user says: ", inputValue);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.min.js"></script>
希望对你有帮助,再见。
关于javascript - 单击 "Cancel"按钮后的 SweetAlert 调用代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44952931/