单击 "Cancel" 按钮后的 SweetAlert 调用代码

SweetAlert Calling code after clicking "Cancel" button

我写了下面的代码,我想在"Cancel"按钮下调用一个代码:

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');
                }
            });
    });

}

但我不能打电话给 "cancel clicked"。我在文档中寻找但找不到解决方案。

您向 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>

希望对你有帮助,再见

你也可以使用 .then

来实现承诺

我给你举个例子,按下按钮后就会执行

swal( 'Error!',valido.msj,'error').then((e)=>{
  if( valido.msj=="Enlace de botón No Válido" ){
     document.getElementById('link_btn_barra').focus();
  } 
});