簡體   English   中英

從甜蜜警報對話框中刪除“確定”按鈕

[英]Remove "OK" button from sweet alert dialog

我正在使用 javascript sweetalert2庫。

我想從警告框中刪除確定按鈕,但我沒有找到任何不顯示此按鈕的屬性。

我正在使用計時器屬性timer:1000在一秒鍾內關閉警報。 所以,我認為在這件事上沒有使用確定按鈕。

在此處輸入圖像描述

您可以使用這些屬性:

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

2018 年 4 月 6 日更新

不再需要 showCancelButton 和 showConfirmButton。 相反,您可以設置 buttons: true 以顯示兩個按鈕,或設置 buttons: false 以隱藏所有按鈕。 默認情況下,僅顯示確認按鈕。

所以現在而不是做

showCancelButton: false;

showConfirmButton: false;

做就是了

buttons: false;

指南

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

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

這是小提琴

甜蜜警報 2 | 2022 更新

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

作為此處的新文檔。

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

        });
}
swal({

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

使用closeOnClickOutside: false,它對我closeOnClickOutside: false,

此已接受的答案已被棄用。 以下是在 SweetAlert2 中隱藏或刪除按鈕的方法。

{
  buttons: false,
}

嘗試將showConfirmButton屬性設置為 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的 v9.x。

重大更改- 將type重命名為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>

標題部分:

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

在添加任何按鈕之前,清除所有按鈕,然后重新添加它們(假設警報名稱為“A”) -

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

希望能幫到你!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM