如何在JavaScript中显示SweetAlert

ash*_*n12 2 javascript sweetalert sweetalert2

如何在此JavaScript代码中显示SweetAlert?

function removeReg(del_reg) {
  if (confirm("Are you sure you want to delete? \n the reg name : " + del_reg)) {
    // Code goes here
  }
}
Run Code Online (Sandbox Code Playgroud)

我只想在if条件下调用SweetAlert ,即在SweetAlert中,我需要显示消息"Are you sure you want to delete?".

Ric*_*ick 6

打电话swal()与您的自定义选项removeReg(del_reg)功能,并使用Promise由归国swal()确定什么是用户的选择:

  • 如果用户点击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');
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<button id="remBtn">Remove Registered Name</button>
Run Code Online (Sandbox Code Playgroud)

更多关于SweetAlert的高级示例