34

我正在使用 javascript sweetalert2库。

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

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

在此处输入图像描述

4

12 回答 12

60

您可以使用这些属性:

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')
    }
  }
)
于 2017-02-23T05:57:15.483 回答
24

2018 年 4 月 6 日更新

不再需要 showCancelButton 和 showConfirmButton。相反,您可以设置按钮:true 以显示两个按钮,或按钮:false 以隐藏所有按钮。默认情况下,仅显示确认按钮。

所以现在而不是做

showCancelButton: false;

showConfirmButton: false;

做就是了

buttons: false;

指南

于 2018-04-06T15:00:09.810 回答
5

您需要showConfirmButton:false在配置中进行设置。

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

这是小提琴

于 2017-02-23T05:58:05.750 回答
3
swal({

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

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

于 2018-05-28T06:31:00.530 回答
3

这对我有用:$(".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'); 

        });
}
于 2017-11-01T15:34:25.920 回答
2

这个接受的答案已被弃用。这是在 SweetAlert2 中隐藏或删除按钮的方法。

{
  buttons: false,
}
于 2020-05-01T03:22:44.923 回答
1

另一种方法可以做到这一点。

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.xSweetAlert2。

重大变化- 重命名typeicon

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>

于 2019-09-30T15:19:32.300 回答
1

甜蜜警报 2 | 2022 更新

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

作为这里的新文档。

于 2022-02-11T07:41:17.807 回答
0

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

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

希望它会有所帮助!!!

于 2017-05-13T15:25:37.877 回答
0

尝试将showConfirmButton属性设置为 false。

看看他们的文档

于 2017-02-23T05:55:34.650 回答
0

下面的代码对我有用

我只设置了buttons: false;

并更新

swal({
    title: 'Auto close alert!',
    text: 'I will close in 2 seconds.',
    timer: 2000,
    showCancelButton: false,
    showConfirmButton: false
});
于 2018-09-05T11:36:25.787 回答
0

标题部分:

<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'
    )   } }) });
于 2021-06-18T04:45:30.817 回答