【问题标题】:Sweet Alert confirmation before delete删除前的甜蜜警报确认
【发布时间】:2017-09-04 09:59:39
【问题描述】:

您好,我是使用甜美警报 js 使我的警报框更加精美的新手。我正在使用普通的 javascript 警报确认来删除表中的特定数据。但是,当我尝试在删除它之前运行一个甜蜜的警报确认时,会删除文件而不会弹出确认。

下面是我的 JS 中的代码。

<script>
    archiveFunction() {
        swal({
  title: "Are you sure?",
  text: "But you will still be able to retrieve this file.",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, archive it!",
  cancelButtonText: "No, cancel please!",
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    swal("Deleted!", "Your imaginary file has been archived.", "success");
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});
    }
</script>

这是我下面的 html 表单。

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <input type="hidden" name="p_id" id="p_id" value="<?php echo $rows['p_id']; ?>">
        <button class="btn btn-danger" type="submit" name="archive" onclick="archiveFunction()">
            <i class="fa fa-archive"></i>
                Archive
        </button>
</form>

这是php中的代码。

<?php

include('../config.php');

if(isset($_POST['archive']))
{
$p_id = $_REQUEST['p_id'];
// sql to delete a record
$sqli = "UPDATE students SET archive = 2 WHERE p_id=$p_id";

if ($conn->query($sqli) === TRUE) {
    echo "swal('Archived!', 'Click ok to see!', 'success')";
} else {
    echo "Error Archiving record: " . $conn->error;
}

$conn->close(); 
}
?>

我想要发生的是在删除特定数据之前确认甜蜜警报确认。

【问题讨论】:

    标签: javascript php jquery sweetalert


    【解决方案1】:

    正如其他提到的,您的按钮单击将表单提交给指定的操作,您无法在警报中选择您的选项。因此,您应该使用event.preventDefault() 阻止表单提交,并且仅在用户按下是时才提交表单。

    function archiveFunction() {
    event.preventDefault(); // prevent form submit
    var form = event.target.form; // storing the form
            swal({
      title: "Are you sure?",
      text: "But you will still be able to retrieve this file.",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, archive it!",
      cancelButtonText: "No, cancel please!",
      closeOnConfirm: false,
      closeOnCancel: false
    },
    function(isConfirm){
      if (isConfirm) {
        form.submit();          // submitting the form when user press yes
      } else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");
      }
    });
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
    <form action="abc" method="POST">
            <input type="hidden" name="p_id" id="p_id" value="<?php echo $rows['p_id']; ?>">
            <button class="btn btn-danger" name="archive" type="submit" onclick="archiveFunction()">
                <i class="fa fa-archive"></i>
                    Archive
            </button>
    </form>

    【讨论】:

    • 我试过了。顺便说一句,是 abc 动作吗?如果我点击是,什么都不会发生。
    • abc 只是一个虚拟动作。用你的行动代替它。在这个例子中,它路由到 abc,按下是,我们将得到一个 404,因为 abc 只是一个虚拟 url
    • 我尝试更改它,但仍然不会提交。如果你有空,我们可以在聊天室继续讨论吗?
    • 当然可以讨论一下
    • 抱歉,我可能在星期四的连接速度很慢。顺便说一句,我在创建要存档的表单的同一页面中使用 isset 帖子。我没有使用任何动作,只使用 php 服务器作为它自己。如果不执行表单中的操作,我该如何做到这一点。
    【解决方案2】:

    此删除确认代码在 express js 中带有甜蜜警报

    我的代码:

    function validateForm() {
          event.preventDefault(); // prevent form submit
          var form = document.forms["myForm"]; // storing the form
          swal({
                 title: "Are you sure?",
                 text: "Once deleted, you will not be able to recover this imaginary file!",
                 icon: "warning",
                 buttons: true,
                 dangerMode: true,
               })
              .then((willDelete) => {
                   if (willDelete) {
                         form.submit();
                   } else {
                          swal("Your imaginary file is safe!");
               }
            });
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
    <form name="myForm"  method="POST" action="/delete">
          <input type="hidden" value="<%= alldata[i]._id %>" name="std_id"> 
          <input type="submit" onclick="validateForm()" class="btn btn-danger btn-xs" value="DELETE">
    </form>

    【讨论】:

      【解决方案3】:

      发生这种情况是因为您没有通过相应地使用 event.preventDefault()returnfalsetrue 来阻止浏览器的默认行为。

      function [...](e) {
          e.preventDefault();
          [...]
      }
      

      改变,

      <button class="btn btn-danger" type="submit" name="archive" onclick="archiveFunction()">
      

      到,

      <button class="btn btn-danger" type="submit" name="archive" onclick="archiveFunction(event)">
      

      为内联事件侦听器传递事件参数。

      或者如果不需要form,那么您可以删除form 标签。

      【讨论】:

        【解决方案4】:

        删除&lt;form&gt; 并替换:

        <button class="btn btn-danger" type="submit">
        

        到:

        <button class="btn btn-danger ajax_delete" type="button">
        

        通过“ajax_delete”调用onclick函数

        $(".ajax_delete").on("click", function (){ /*your sweet alert code and after ajax call function */});
        

        然后将您的删除代码放入 ajax 调用文件。

        【讨论】:

          【解决方案5】:

          function archiveFunction() {
          event.preventDefault(); // prevent form submit
          var form = event.target.form; // storing the form
                  swal({
            title: "Are you sure?",
            text: "But you will still be able to retrieve this file.",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, archive it!",
            cancelButtonText: "No, cancel please!",
            closeOnConfirm: false,
            closeOnCancel: false
          },
          function(isConfirm){
            if (isConfirm) {
              form.submit();          // submitting the form when user press yes
            } else {
              swal("Cancelled", "Your imaginary file is safe :)", "error");
            }
          });
          }
          <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
          <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
          <form action="abc" method="POST">
                  <input type="hidden" name="p_id" id="p_id" value="<?php echo $rows['p_id']; ?>">
                  <button class="btn btn-danger" name="archive" type="submit" onclick="archiveFunction()">
                      <i class="fa fa-archive"></i>
                          Archive
                  </button>
          </form>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-04-03
            • 2017-01-04
            • 2015-09-17
            • 1970-01-01
            • 2023-03-08
            • 2017-08-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多