【问题标题】:How to show SweetAlert in JavaScript如何在 JavaScript 中显示 SweetAlert
【发布时间】:2018-07-29 11:21:38
【问题描述】:

如何在这段 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?"

【问题讨论】:

    标签: javascript sweetalert sweetalert2


    【解决方案1】:

    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>

    More on the advanced examples of SweetAlert

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 2018-11-30
      • 2017-12-04
      • 2021-09-26
      相关资源
      最近更新 更多