从甜蜜警报对话框中删除 "OK" 按钮

Remove "OK" button from sweet alert dialog

我正在使用 javascript sweetalert2 库。

我想从警告框中删除确定按钮,但我没有找到任何属性不显示此按钮。

我正在使用计时器 属性 timer:1000 在一秒钟内关闭警报。 所以,我认为在这件事上没有使用确定按钮。

尝试将 showConfirmButton 属性 设置为 false。

Look at their docs

您可以使用这些属性:

showCancelButton: false, // There won't be any cancel button
showConfirmButton: false // There won't be any confirm button

像这样

swal({
  title: 'Auto close alert!',
  text: 'I will close in 2 seconds.',
  timer: 2000,
  showCancelButton: false,
  showConfirmButton: false
}).then(
  function () {},
  // handling the promise rejection
  function (dismiss) {
    if (dismiss === 'timer') {
      //console.log('I was closed by the timer')
    }
  }
)

您需要在配置中设置 showConfirmButton:false

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showConfirmButton:false,
  confirmButtonText: 'Yes, delete it!'
})

这是fiddle

在添加任何按钮之前,清除所有按钮,然后重新添加它们(假设警报名称是 'A')-

A.getButtonTypes().clear();
ButtonType OpenStorage=new ButtonType("Open Storage");
A.getButtonTypes().addAll(OpenStorage,ButtonType.CANCEL,ButtonType.NEXT);

希望对您有所帮助!!!

这对我有用:$(".confirm").attr('disabled', 'disabled');

我的函数:

function DeleteConfirm(c){
  swal({   
            title: "Want to delete this item?",   
            text: "You will not be able to undo this action!",   
            type: "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            closeOnConfirm: false 
        }, function(){ 
          $(".confirm").attr('disabled', 'disabled'); 

        });
}

更新 2018 年 4 月 6 日

showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons, or buttons: false to hide all buttons. By default, only the confirm button is shown.

所以现在不用

showCancelButton: false;

showConfirmButton: false;

随心所欲

buttons: false;

Guides

swal({

    title: "Success",
    text: "Permissions assigned Successfully",
    icon: "success",
    closeOnClickOutside: false,
})

使用closeOnClickOutside: false, 它对我有用。

下面的代码适合我

我只设置了buttons: false;

并更新

swal({
    title: 'Auto close alert!',
    text: 'I will close in 2 seconds.',
    timer: 2000,
    showCancelButton: false,
    showConfirmButton: false
});

还有一种方法可以做到这一点。

Swal.fire({
  type: 'error',
  title: 'Cancelled',
  text: 'Your offer is safe ',
  showConfirmButton: false,
  timer: 2000
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>


正在升级到 v9.x 的 SweetAlert2。

Breaking change - rename type to icon

Swal.fire({
  icon: 'error',
  title: 'Cancelled',
  text: 'Your offer is safe ',
  showConfirmButton: false,
  timer: 2000
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

这个接受的答案已被弃用。以下是如何在 SweetAlert2 中隐藏或删除按钮。

{
  buttons: false,
}

页眉部分:

<link rel="stylesheet" href="https://sweetalert2.github.io/styles/bootstrap4-buttons.css">

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

正文部分添加按钮:

<a href="" id="delete" class="btn btn-danger">Delete</a>

jQuery 部分:

$(document).on("click", "#delete", function (e) {
        e.preventDefault();
        var link = $(this).attr("href"); const swalWithBootstrapButtons = swal.mixin({   customClass: {
    confirmButton: 'btn btn-success',
    cancelButton: 'btn btn-danger'   },   buttonsStyling: false }) swalWithBootstrapButtons.fire({   title: 'Are you sure?',   text: "You won't be able to revert this!",   icon: 'warning',   showCancelButton: true,   confirmButtonText: 'Yes, delete it!',   cancelButtonText: 'No, cancel!',   reverseButtons: true }).then((result) => {   if (result.isConfirmed) {
    swalWithBootstrapButtons.fire(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )   } else if (
    /* Read more about handling dismissals below */
    result.dismiss === swal.DismissReason.cancel   ) {
    swalWithBootstrapButtons.fire(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )   } }) });

甜蜜警报 2 | 2022 年更新

Swal.fire({
    ...
    showConfirmButton: false, //hide OK button
    allowOutsideClick: false, //optional, disable outside click for close the modal
    ...
});

作为新文档 here