问题
How can I show SweetAlert in this JavaScript code?
function removeReg(del_reg) {
if (confirm("Are you sure you want to delete? \n the reg name : " + del_reg)) {
// Code goes here
}
}
I just want to call SweetAlert in the if
condition, i.e. in SweetAlert, I need to show the message "Are you sure you want to delete?"
.
回答1:
Call swal() with your custom options in the removeReg(del_reg)
function and use the Promise returned by swal()
to determine what is the user's choice:
- If the user clicked
YES
, the returnedvalue
will betrue
- If the user clicked
NO
or pressed the Esc key, the returnedvalue
will benull
var button = document.getElementById('remBtn')
function removeReg(del_reg) {
swal({
text: "Are you sure you want to delete? \n the reg name : " + del_reg,
icon: "error",
buttons: ['NO', 'YES'],
dangerMode: true
})
.then(function(value) {
console.log('returned value:', value);
});
}
button.addEventListener('click', function() {
console.log('clicked on button')
removeReg('SomeCustomRegName');
});
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<button id="remBtn">Remove Registered Name</button>
More on the advanced examples of SweetAlert
来源:https://stackoverflow.com/questions/51579801/how-to-show-sweetalert-in-javascript