如何在此JavaScript代码中显示SweetAlert?
function removeReg(del_reg) {
if (confirm("Are you sure you want to delete? \n the reg name : " + del_reg)) {
// Code goes here
}
}
我只想在if
条件下调用SweetAlert,即在SweetAlert中,我需要显示消息"Are you sure you want to delete?"
。
答案 0 :(得分:6)
在removeReg(del_reg)
函数中调用swal()
with your custom options,然后使用swal()
返回的Promise
来确定用户的选择:
YES
,则返回的value
将是true
NO
或按 Esc 键,则返回的value
将是null
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>