如何按esc键关闭甜蜜警报?

时间:2017-12-16 12:02:38

标签: javascript jquery html sweetalert2

我正在使用甜蜜警报2,我有类似的东西。

swal

及其实施

$('#btn_undispense').click(function() {
        swal({
            title: "Are you want to unDispense this claim?",
            //text: 'you want to unDispense this claim ?',
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: 'btn-warning',
            cancelButtonClass: 'btn-default',
            confirmButtonText: "UnDispense",
            cancelButtonText: "UnDispense & Delete",
            closeOnConfirm: true,
            closeOnCancel: true,
            allowEscapeKey: true,
            allowOutsideClick: true
        }, function(isConfirm) {
            var url = '@Url.Action("UnDispenseAndDelete", "ClaimData")';
            $.ajax({
                type: 'GET',
                url: url,
                data: {
                    par:'@Model.ParKey',
                    par2: '@Model.ParKey2',
                    isUnDispensedOnly:isConfirm
                },
                success: function(res) {
                    debugger;
                    //1 undispensedOnly   //2 undispensed and delete
                    if (res === "1") {
                        showSuccessToast("Claim undispensed successfully");
                        if (@((int)Model.UserSession.UserRoleEnum) === 3) {

                            var url1 = "/Pharmacist/ClaimData/OpenClaim?par=" + '@Model.ParKey' + "&par2=" + '@Model.ParKey2' + "&par7=" + '@Model.ParKey7' + "&isClaimSearchPayer="+true ;
                            window.location.href = url1;
                        } else {
                            var url = "/Pharmacist/ClaimData/OpenClaim?par=" + '@Model.ParKey' + "&par2=" + '@Model.ParKey2' + "&par7=" + '@Model.ParKey7' + "";
                            window.location.href = url;
                        }
                    }else if(res === "2"){
                        var url1 = '@Url.Action("ClaimSearch", "Batch", new {area="Pharmacist"})';
                        window.location.href = url1;
                    }
                    else {
                        //  showErrorToast("Error");
                        showPopupModal("Errors", '@Url.Action("PopupError")');
                    }
                },
                failure: function () {
                    showErrorToast("Internal Server Error");
                }
            });

        });
    });

我想通过按ESC键关闭这个甜蜜警报,而不会激活两个选项中的一个,所以我添加了allowEscapeKey和allowOutsideClick选项,allowOutsideClick选项正常工作问题是使用ESC键,每当我按下ESC它就会执行取消按钮是UnDispense&删除,我只是想关闭swal而不是没有激励。

2 个答案:

答案 0 :(得分:0)

它可能有帮助

  $('body').keypress(function(e){ 
  alert(e.which); 
  if(e.which == 27){
  swal.close();
   } 
  });

答案 1 :(得分:0)

尝试这个

 $(document).on('keypress', function (e) {
            if (e.which == 27) {
                swal.closeModal();
            }
        });
相关问题