[英]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;
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。
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
toicon
重大更改- 将
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.