How to show SweetAlert in JavaScript

邮差的信 提交于 2019-12-11 04:26:01

问题


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 returned value will be true
  • If the user clicked NO or pressed the Esc key, the returned value will be 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>

More on the advanced examples of SweetAlert



来源:https://stackoverflow.com/questions/51579801/how-to-show-sweetalert-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!