簡體   English   中英

使用Bootstrap和AJAX刪除確認模式

[英]Delete confirmation modal using Bootstrap and AJAX

我正在嘗試創建一個模態彈出窗口,並在模態主體中包含一個AJAX調用。 該腳本包括AJAX。 工作正常。

這是調用模式的按鈕(我正在使用TWIG模板)

<button class="btn btn-danger" data-id="{{ product.id }}">Delete</button>

這是jQuery腳本:

$(document).ready(function() {
  $('[data-id]').each(function() {
    var $this = $(this),
      id = $this.data('id');
    $this.on('click', function(event) {
      event.preventDefault();
      $.post('{{ path('dashboard_ajax ') }}', {
          'id': id
        },
        function(details) {
          var $modal = $(details);
          $('#confirm-delete').remove();
          $('body').append($modal);
          $modal.modal();
        }
      );
    })
  });
});

這是帶有模態的頁面

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
      </div>

      <div class="modal-body">

        <div class="alert alert-danger" role="alert">

          <p>Do you really want to delete <b>{{ product.name }}?</b></p>

        </div>

        <img src="{{ asset('uploads/product_images/') }}{{ product.image }}" style="width: 240px;" class="img-responsive thumbnail" />
      </div>

      <div class="modal-footer">
        <button type="button" id="misha" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <a class="btn btn-danger btn-ok">Delete</a>
      </div>
    </div>
  </div>

現在,如果單擊刪除按鈕,我想使用相關頁面刪除所選項目(例如:delete.php)

您可以做的一種方法是,附加一個點擊處理程序,並像這樣設置位置

window.location.href = 'delete.php';

或類似的東西

<a class="btn btn-danger btn-ok" href="delete.php">Delete</a>

如果您具有產品ID,則將其發送至delete.php,如下所示

<a class="btn btn-danger btn-ok" href="delete.php?ID={{ product.id }}">Delete</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM