简体   繁体   English

从甜蜜警报对话框中删除“确定”按钮

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

I am using javascript sweetalert2 library.我正在使用 javascript sweetalert2库。

I want to remove the OK button from the alert box but I did not find any property for not to display this button.我想从警告框中删除确定按钮,但我没有找到任何不显示此按钮的属性。

I am using the timer property timer:1000 for closing the alert in one second.我正在使用计时器属性timer:1000在一秒钟内关闭警报。 So, I don't think there is a use of the ok button in this matter.所以,我认为在这件事上没有使用确定按钮。

在此处输入图像描述

You can use these properties:您可以使用这些属性:

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

Like This像这样

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

Update 4/6/2018 2018 年 4 月 6 日更新

showCancelButton and showConfirmButton are no longer needed.不再需要 showCancelButton 和 showConfirmButton。 Instead, you can set buttons: true to show both buttons, or buttons: false to hide all buttons.相反,您可以设置 buttons: true 以显示两个按钮,或设置 buttons: false 以隐藏所有按钮。 By default, only the confirm button is shown.默认情况下,仅显示确认按钮。

So now instead of doing所以现在而不是做

showCancelButton: false;

showConfirmButton: false;

Just do做就是了

buttons: false;

Guides指南

You need to set showConfirmButton:false in your configuration.您需要在配置中设置showConfirmButton:false

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

Here's the fiddle这是小提琴

SWEET ALERT 2 |甜蜜警报 2 | 2022 UPDATE 2022 更新

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

As new documentation here .作为此处的新文档。

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

My function:我的功能:

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

Use closeOnClickOutside: false, It works for me.使用closeOnClickOutside: false,它对我closeOnClickOutside: false,

This accepted answer has been deprecated.此已接受的答案已被弃用。 Here is how you can hide or remove buttons in SweetAlert2.以下是在 SweetAlert2 中隐藏或删除按钮的方法。

{
  buttons: false,
}

Try setting the showConfirmButton property to false.尝试将showConfirmButton属性设置为 false。

Look at their docs看他们的文档

Below code works for me下面的代码对我有用

I have only set buttons: false;我只设置了buttons: false;

and update并更新

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

One more way to do the same.另一种方法来做同样的事情。

 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>


Upgrading to v9.x of SweetAlert2.升级到v9.x的 v9.x。

Breaking change - rename type to icon重大更改- 将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>

Header Part:标题部分:

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

Body Part Add A Button:正文部分添加按钮:

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

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

Before adding any buttons,clear all the buttons and then re-add them like(assuming the Alert name is 'A') -在添加任何按钮之前,清除所有按钮,然后重新添加它们(假设警报名称为“A”) -

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

Hope it'll help!!!希望能帮到你!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM