1. 程式人生 > 其它 >ajax與sweetalert結合使用

ajax與sweetalert結合使用

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h2 class="text-center">資料展示</h2>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>編號</th>
                    <th>姓名</th>
                    <th>密碼</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                    {% for user_obj in user_queryset %}
                        <tr>
                        <td>{{ user_obj.pk }}</td>
                        <td>{{ user_obj.name }}</td>
                        <td>{{ user_obj.pwd }}</td>
                        <td>
                            <a href="#" class="c1 btn-danger btn btn-xs" delete_id="{{ user_obj.pk }}">刪除</a>
                        </td>
                         </tr>
                    {% endfor %}

                </tbody>
            </table>
        </div>
    </div>
</div>

</body>



    <script>

        $('.c1').click(function (){
             let $delete=$(this)
            swal({
                  title: "確定刪除嗎?",
                  text: "真刪就跑路",
                  type: "warning",
                  showCancelButton: true,
                  confirmButtonClass: "btn-danger",
                  confirmButtonText: "確定刪除",
                  cancelButtonText: "算了",
                  closeOnConfirm: false,
                  closeOnCancel: false
                },
                function(isConfirm) {
                  if (isConfirm) {
                      let deleteId=$delete.attr('delete_id');
                          $.ajax({
                                  url:'',
                                  type:'post',
                                  data:{'deleteId':deleteId},
                                  success:function (args) {
                                      swal(args,'success')
                                      $delete.parent().parent().remove()  
                                      {# 刪除tr這個標籤 #}
                                  }
                              })
                  } else {
                    swal("取消了", "真慫啊", "error");
                  }
                });
        })