【问题标题】:Laravel can't confirm delete with SweetAlertLaravel 无法通过 SweetAlert 确认删除
【发布时间】:2019-05-15 10:47:29
【问题描述】:

我在 Sweetalert 上遇到了这个问题 - 我有以下代码,但确认模式没有用,因为请求通过了它,用户甚至没有时间决定。我必须弄清楚停止请求执行并等待用户决定(确定/取消)。

代码如下:

<a href="{{route('notes.destroy', $note->id)}}" 
data-id="{{$note->id}}" onclick="confirmDelete('{{$note->id}}')" type="submit">
<span class="badge badge-danger">Delete</span></a>

这是 jQuery(我认为这是问题所在):

<script>
function confirmDelete(item_id) {
    swal({ 
        title: "Are you sure?",
        text: "Once deleted, you will not be able to recover it!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
        if (willDelete) {
            $('#' + item_id).submit();
        } else {
            swal("Cancelled Successfully");
        }
    });
}
</script>

【问题讨论】:

    标签: jquery laravel sweetalert


    【解决方案1】:

    在刀片文件中:

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.33.1/sweetalert2.js"></script>
    
    <form id="delete_from_{{$note->id}}" method="POST" action="{{ route('post.destroy', $note->id) }}">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}
    
        <div class="form-group">
            <a href="javascript:void(0);" data-id="{{$note->id}}" class="_delete_data">
                <span class="badge badge-danger">Delete</span>
            </a>                    
        </div>
    </form>
    

    js代码:

    <script>
        $(document).ready(function(){
            $('._delete_data').click(function(e){
                var data_id = $(this).attr('data-id');
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!'
                }).then((result) => {
    
                    if (result.value) {
                        $(document).find('#delete_from_'+data_id).submit();
                    }
                })
            });
        });            
    </script>
    

    【讨论】:

      【解决方案2】:

      如果我正确理解您的问题,您的意思是 sweetalert 模式没有为您提供在 onclick 事件之后是继续还是中止决定的任何选项。尝试如下添加“showCancelButton”和“confirmButtonText”,看看是否有什么不同。

      swal({
          title: "Are you sure?",
          text: "Once deleted, you will not be able to recover it!",
          icon: "warning",
          showCancelButton: true,
          confirmButtonText: 'ok'
      }).then((willDelete) => {
          if (willDelete) {
              $('#' + item_id).submit();
          } else {
              swal("Cancelled Successfully");
          }
      });
      

      【讨论】:

      • 不,问题是这样的:&lt;a href="{{route('notes.destroy', $note-&gt;id)}}" 在点击时,它会在用户接受或取消模式之前发送删除条目的请求。我删除了路由并放置了&lt;a href="#"&gt;,但现在我需要将取消和确定按钮映射到 ajax 调用。有什么想法吗?
      • @Cicharito 你如何在 if(willDelete){ } 条件中包含路由重定向?这样,模态将始终首先触发,然后仅根据您对 sweetalert modal 的操作选择进行下一个操作
      【解决方案3】:

      使用preventDefault()防止表单提交

      function confirmDelete(event,item_id) {
      event.preventDefault();
          swal({ 
              title: "Are you sure?",
              text: "Once deleted, you will not be able to recover it!",
              icon: "warning",
              buttons: true,
              dangerMode: true,
          })
          .then((willDelete) => {
              if (willDelete) {
                  $('#' + item_id).submit();
              } else {
                  swal("Cancelled Successfully");
              }
          });
      }
      <a href="{{route('notes.destroy', $note->id)}}" 
      data-id="{{$note->id}}" onclick="confirmDelete(event,'{{$note->id}}')" type="submit">
      <span class="badge badge-danger">Delete</span></a>
      and here's the jQuery (which I think it's the problem):

      【讨论】:

      • 奇怪,这应该可以,你是否将事件参数添加到你的函数中?
      猜你喜欢
      • 1970-01-01
      • 2021-02-25
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2022-08-14
      • 2020-03-11
      相关资源
      最近更新 更多