javascript - 如何在 JavaScript 中显示 SweetAlert

标签 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
  }
}

我只想在 if 条件下调用 SweetAlert,即在 SweetAlert 中,我需要显示消息 “您确定要删除吗?”

最佳答案

调用swal() with your custom optionsremoveReg(del_reg) 函数中使用 Promiseswal() 返回以确定用户的选择:

  • 如果用户点击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>

More on the advanced examples of SweetAlert

关于javascript - 如何在 JavaScript 中显示 SweetAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579801/

相关文章:

javascript - 如何使用 NodeJS 创建流式 API

javascript - 闪烁的页面标题

javascript - 为什么Ajax和SweetAlert不显示?

html - 是否可以在甜蜜警报 2 中添加整个 Angular 组件?

javascript - SweetAlert isConfirm 无法正常工作

javascript - 为什么这段代码循环 16 次而不是 8 次?

javascript - 覆盖 npm 包依赖

javascript - 我怎样才能获得甜蜜警报弹出消息内容的值(value)?

vue.js - 如何在 SweetAlert2 中使用 v-for

angular - 我如何模拟 sweetalert 以及如何使其在 Angular Testing 中返回 true