繁体   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