sweetalert 2 上有超过 2 个按钮
More than 2 buttons on sweetalert 2
我有一个带 2 个按钮的 sweetalert,但我想在其中多一个按钮。
例如,截至目前,我有“是”和“否”,我想再添加一个按钮,请稍后再说。请帮忙。
$("#close_account").on("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "You will not be able to open your account!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, close my account!",
closeOnConfirm: false
},
function() {
window.location.href="<?php echo base_url().'users/close_account' ?>"
});
});
提前致谢。
您应该使用带有 jQuery 事件绑定的自定义 HTML,它的工作原理几乎相同,唯一的问题是您需要自己为按钮添加样式,因为 SweetAlert 类 不不适合我。
$(document).ready(function() {
$("#close_account").on("click", function(e) {
var buttons = $('<div>')
.append(createButton('Ok', function() {
swal.close();
console.log('ok');
})).append(createButton('Later', function() {
swal.close();
console.log('Later');
})).append(createButton('Cancel', function() {
swal.close();
console.log('Cancel');
}));
e.preventDefault();
swal({
title: "Are you sure?",
html: buttons,
type: "warning",
showConfirmButton: false,
showCancelButton: false
});
});
});
function createButton(text, cb) {
return $('<button>' + text + '</button>').on('click', cb);
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="close_account">Show</button>
上面的答案对我不起作用,所以我做了以下操作:
$(document).on('click', '.SwalBtn1', function() {
//Some code 1
console.log('Button 1');
swal.clickConfirm();
});
$(document).on('click', '.SwalBtn2', function() {
//Some code 2
console.log('Button 2');
swal.clickConfirm();
});
$('#ShowBtn').click(function(){
swal({
title: 'Title',
html: "Some Text" +
"<br>" +
'<button type="button" role="button" tabindex="0" class="SwalBtn1 customSwalBtn">' + 'Button1' + '</button>' +
'<button type="button" role="button" tabindex="0" class="SwalBtn2 customSwalBtn">' + 'Button2' + '</button>',
showCancelButton: false,
showConfirmButton: false
});
});
.customSwalBtn{
background-color: rgba(214,130,47,1.00);
border-left-color: rgba(214,130,47,1.00);
border-right-color: rgba(214,130,47,1.00);
border: 0;
border-radius: 3px;
box-shadow: none;
color: #fff;
cursor: pointer;
font-size: 17px;
font-weight: 500;
margin: 30px 5px 0px 5px;
padding: 10px 32px;
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="ShowBtn" class="customSwalBtn">Alert</button>
Richard Cook 在上面评论说原始答案(由 Konstantin Azizov 提供)不再适用于 SweetAlert2 的 4.2.6 版。他建议这与节点被克隆有关,因为它们被添加到 html。我不太了解 SweetAlert2,无法判断他是否正确。不过,我可以看到我的按钮已添加,但从未调用过 onclick 回调函数。
经过一些努力,我能够让它与当前版本的 SweetAlert2 一起使用。为了让它工作,我必须稍后将 onclick 事件分配给按钮。我最终为按钮添加了 id,使它们很容易从 jQuery 变为 select。然后我将 onOpen 函数添加到我的 swal 对象,并在其中连接逻辑以关联回调函数。下面是对我有用的代码片段。
另请注意,消息和按钮使用了一些现有的 SweetAlert2 classes,因此它们与现有的 UI 元素具有相同的外观。请注意,我确实尝试使用 swal2-confirm 和 swal2-cancel classes。当我这样做时,我 运行 遇到了一些问题。可能是 SweetAlert2 代码依赖于只有一个元素使用 class。我没有时间追查它,所以我就停止使用那些 classes。
function createButton(text, id) {
return $('<button class="swal2-input swal2-styled" id="'+id+'">'+text+'</button>');
}
function createMessage(text) {
return $('<div class="swal2-content" style="display: block;">'+text+'</div>');
}
function swThreeButton(msg, textOne, textTwo, textThree, callbackOne, callbackTwo, callbackThree) {
var buttonsPlus = $('<div>')
.append(createMessage(msg))
.append(createButton(textOne,'sw_butt1'))
.append(createButton(textTwo,'sw_butt2'))
.append(createButton(textThree,'sw_butt3'));
swal({
title: 'Select Option',
html: buttonsPlus,
type: 'question',
showCancelButton: false,
showConfirmButton: false,
animation: false,
customClass: "animated zoomIn",
onOpen: function (dObj) {
$('#sw_butt1').on('click',function () {
swal.close();
callbackOne();
});
$('#sw_butt2').on('click',function () {
swal.close();
callbackTwo();
});
$('#sw_butt3').on('click',function () {
swal.close();
callbackThree();
});
}
});
};
我曾经需要将第三个按钮添加到 SweetAlert2 弹出窗口。在我的例子中,最简单的方法是使用 footer
,它可以是纯文本或 HTML:
$(function() {
$('.btn').click(function(e) {
e.preventDefault();
Swal.fire({
icon: 'warning',
title: 'Are you sure?',
text: 'You won\'t be able to revert this!',
showCloseButton: true,
showCancelButton: true,
footer: '<a href="#!" id="some-action">Some action</a>'
}).then((result) => {
console.log(result);
});
});
$(document).on('click', '#some-action', function(e) {
e.preventDefault();
console.log('Some action triggered.');
});
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<div class="text-center">
<button type="button" class="btn btn-primary my-3">Click me</button>
</div>
对于 sweetalert2
,这对我有用:(Run it live)
var onBtnClicked = (btnId) => {
Swal.close();
if (btnId != "cancel") Swal.fire("you choosed: " + btnId);
};
Swal.fire({
title: "What you want to do?",
icon: "warning",
showConfirmButton: false,
showCloseButton: true,
html: `
<p>select an action</p>
<div>
<button class="btn btn-primary" onclick="onBtnClicked('reply')">Reply</button>
<button class="btn btn-danger" onclick="onBtnClicked('delete')">Delete</button>
<button class="btn btn-secondary" onclick="onBtnClicked('cancel')">Cancel</button>
</div>`
});
对于 sweetalert2 这对我有用
$(document).ready(function() {
$("#close_account").on("click", function(e) {
var buttons = $('<div>')
.append(createButton('Ok', function() {
swal.close();
console.log('ok');
})).append(createButton('Later', function() {
swal.close();
console.log('Later');
})).append(createButton('Cancel', function() {
swal.close();
console.log('Cancel');
}));
e.preventDefault();
swal({
title: "Are you sure?",
html: buttons,
type: "warning",
showConfirmButton: false,
showCancelButton: false
});
});
});
function createButton(text, cb) {
return $('<button>' + text + '</button>').on('click', cb);
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="close_account">Show</button>
我们最近发布了 SweetAlert2 v10,支持第三个“拒绝”按钮:
Swal.fire({
title: 'Do you want to save the changes?',
showDenyButton: true,
showCancelButton: true,
confirmButtonText: `Save`,
denyButtonText: `Don't save`,
}).then((result) => {
if (result.isConfirmed) {
Swal.fire('Saved!', '', 'success')
} else if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
现在使用 ES6 非常容易。
请看这里:如何添加 Unlimited 按钮
createSwalButton = async () => {
let swalButton = await swal({
title: "Add Buttons?",
text: "This will create a new button.",
icon: "info",
buttons: {
buttonOne: {
text: "First",
value: "firstVal",
visible: true,
className: "swal-btn-cancel",
closeModal: true,
},
buttonTwo: {
text: "Second",
value: "secondVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonThree: {
text: "Third",
value: "thirdVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonFour: {
text: "Fourth",
value: "fourthVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonFive: {
text: "Fifth",
value: "fifthVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
}
},
})
if (swalButton === "firstVal") {
//do stuff
}
}
好吧,我可能有点过分了,但你知道它是如何工作的。
向 SWAL 团队致敬!这是一个很棒的产品。
我有一个带 2 个按钮的 sweetalert,但我想在其中多一个按钮。 例如,截至目前,我有“是”和“否”,我想再添加一个按钮,请稍后再说。请帮忙。
$("#close_account").on("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "You will not be able to open your account!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, close my account!",
closeOnConfirm: false
},
function() {
window.location.href="<?php echo base_url().'users/close_account' ?>"
});
});
提前致谢。
您应该使用带有 jQuery 事件绑定的自定义 HTML,它的工作原理几乎相同,唯一的问题是您需要自己为按钮添加样式,因为 SweetAlert 类 不不适合我。
$(document).ready(function() {
$("#close_account").on("click", function(e) {
var buttons = $('<div>')
.append(createButton('Ok', function() {
swal.close();
console.log('ok');
})).append(createButton('Later', function() {
swal.close();
console.log('Later');
})).append(createButton('Cancel', function() {
swal.close();
console.log('Cancel');
}));
e.preventDefault();
swal({
title: "Are you sure?",
html: buttons,
type: "warning",
showConfirmButton: false,
showCancelButton: false
});
});
});
function createButton(text, cb) {
return $('<button>' + text + '</button>').on('click', cb);
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="close_account">Show</button>
上面的答案对我不起作用,所以我做了以下操作:
$(document).on('click', '.SwalBtn1', function() {
//Some code 1
console.log('Button 1');
swal.clickConfirm();
});
$(document).on('click', '.SwalBtn2', function() {
//Some code 2
console.log('Button 2');
swal.clickConfirm();
});
$('#ShowBtn').click(function(){
swal({
title: 'Title',
html: "Some Text" +
"<br>" +
'<button type="button" role="button" tabindex="0" class="SwalBtn1 customSwalBtn">' + 'Button1' + '</button>' +
'<button type="button" role="button" tabindex="0" class="SwalBtn2 customSwalBtn">' + 'Button2' + '</button>',
showCancelButton: false,
showConfirmButton: false
});
});
.customSwalBtn{
background-color: rgba(214,130,47,1.00);
border-left-color: rgba(214,130,47,1.00);
border-right-color: rgba(214,130,47,1.00);
border: 0;
border-radius: 3px;
box-shadow: none;
color: #fff;
cursor: pointer;
font-size: 17px;
font-weight: 500;
margin: 30px 5px 0px 5px;
padding: 10px 32px;
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="ShowBtn" class="customSwalBtn">Alert</button>
Richard Cook 在上面评论说原始答案(由 Konstantin Azizov 提供)不再适用于 SweetAlert2 的 4.2.6 版。他建议这与节点被克隆有关,因为它们被添加到 html。我不太了解 SweetAlert2,无法判断他是否正确。不过,我可以看到我的按钮已添加,但从未调用过 onclick 回调函数。
经过一些努力,我能够让它与当前版本的 SweetAlert2 一起使用。为了让它工作,我必须稍后将 onclick 事件分配给按钮。我最终为按钮添加了 id,使它们很容易从 jQuery 变为 select。然后我将 onOpen 函数添加到我的 swal 对象,并在其中连接逻辑以关联回调函数。下面是对我有用的代码片段。
另请注意,消息和按钮使用了一些现有的 SweetAlert2 classes,因此它们与现有的 UI 元素具有相同的外观。请注意,我确实尝试使用 swal2-confirm 和 swal2-cancel classes。当我这样做时,我 运行 遇到了一些问题。可能是 SweetAlert2 代码依赖于只有一个元素使用 class。我没有时间追查它,所以我就停止使用那些 classes。
function createButton(text, id) {
return $('<button class="swal2-input swal2-styled" id="'+id+'">'+text+'</button>');
}
function createMessage(text) {
return $('<div class="swal2-content" style="display: block;">'+text+'</div>');
}
function swThreeButton(msg, textOne, textTwo, textThree, callbackOne, callbackTwo, callbackThree) {
var buttonsPlus = $('<div>')
.append(createMessage(msg))
.append(createButton(textOne,'sw_butt1'))
.append(createButton(textTwo,'sw_butt2'))
.append(createButton(textThree,'sw_butt3'));
swal({
title: 'Select Option',
html: buttonsPlus,
type: 'question',
showCancelButton: false,
showConfirmButton: false,
animation: false,
customClass: "animated zoomIn",
onOpen: function (dObj) {
$('#sw_butt1').on('click',function () {
swal.close();
callbackOne();
});
$('#sw_butt2').on('click',function () {
swal.close();
callbackTwo();
});
$('#sw_butt3').on('click',function () {
swal.close();
callbackThree();
});
}
});
};
我曾经需要将第三个按钮添加到 SweetAlert2 弹出窗口。在我的例子中,最简单的方法是使用 footer
,它可以是纯文本或 HTML:
$(function() {
$('.btn').click(function(e) {
e.preventDefault();
Swal.fire({
icon: 'warning',
title: 'Are you sure?',
text: 'You won\'t be able to revert this!',
showCloseButton: true,
showCancelButton: true,
footer: '<a href="#!" id="some-action">Some action</a>'
}).then((result) => {
console.log(result);
});
});
$(document).on('click', '#some-action', function(e) {
e.preventDefault();
console.log('Some action triggered.');
});
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<div class="text-center">
<button type="button" class="btn btn-primary my-3">Click me</button>
</div>
对于 sweetalert2
,这对我有用:(Run it live)
var onBtnClicked = (btnId) => {
Swal.close();
if (btnId != "cancel") Swal.fire("you choosed: " + btnId);
};
Swal.fire({
title: "What you want to do?",
icon: "warning",
showConfirmButton: false,
showCloseButton: true,
html: `
<p>select an action</p>
<div>
<button class="btn btn-primary" onclick="onBtnClicked('reply')">Reply</button>
<button class="btn btn-danger" onclick="onBtnClicked('delete')">Delete</button>
<button class="btn btn-secondary" onclick="onBtnClicked('cancel')">Cancel</button>
</div>`
});
对于 sweetalert2 这对我有用
$(document).ready(function() {
$("#close_account").on("click", function(e) {
var buttons = $('<div>')
.append(createButton('Ok', function() {
swal.close();
console.log('ok');
})).append(createButton('Later', function() {
swal.close();
console.log('Later');
})).append(createButton('Cancel', function() {
swal.close();
console.log('Cancel');
}));
e.preventDefault();
swal({
title: "Are you sure?",
html: buttons,
type: "warning",
showConfirmButton: false,
showCancelButton: false
});
});
});
function createButton(text, cb) {
return $('<button>' + text + '</button>').on('click', cb);
}
<link href="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.2.4/sweetalert2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="close_account">Show</button>
我们最近发布了 SweetAlert2 v10,支持第三个“拒绝”按钮:
Swal.fire({
title: 'Do you want to save the changes?',
showDenyButton: true,
showCancelButton: true,
confirmButtonText: `Save`,
denyButtonText: `Don't save`,
}).then((result) => {
if (result.isConfirmed) {
Swal.fire('Saved!', '', 'success')
} else if (result.isDenied) {
Swal.fire('Changes are not saved', '', 'info')
}
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
现在使用 ES6 非常容易。
请看这里:如何添加 Unlimited 按钮
createSwalButton = async () => {
let swalButton = await swal({
title: "Add Buttons?",
text: "This will create a new button.",
icon: "info",
buttons: {
buttonOne: {
text: "First",
value: "firstVal",
visible: true,
className: "swal-btn-cancel",
closeModal: true,
},
buttonTwo: {
text: "Second",
value: "secondVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonThree: {
text: "Third",
value: "thirdVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonFour: {
text: "Fourth",
value: "fourthVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
},
buttonFive: {
text: "Fifth",
value: "fifthVal",
visible: true,
className: "swal-btn-confirm",
closeModal: true
}
},
})
if (swalButton === "firstVal") {
//do stuff
}
}
好吧,我可能有点过分了,但你知道它是如何工作的。
向 SWAL 团队致敬!这是一个很棒的产品。