1

首先,javascript确认不是我在这里寻找的。请阅读全文。即在每个链接上放置一个 onClick 属性,并带有一个传递给它的函数,该函数通过警告一个正常的系统对话框来确认用户不是我想要的。

我使用了引导程序的对话框,而不是系统提供的常规对话框。

这是不同项目的几个删除按钮

<a href="www.mysite.com/product/1" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>
<a href="www.mysite.com/product/2" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>
<a href="www.mysite.com/product/3" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>

下面是使用 Bootstrap 显示模态的标记。

<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Confirm Delete ?</h3>
  </div>
<div class="modal-body">
  <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to Delete the Category and all its associated Properties?</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
  </div>
</div>

对话框显示正确,但在按下对话框中的删除按钮后,我无法重定向到与上述链接对应的页面,模式只是消失而没有重定向。

4

3 回答 3

7

只需将类添加remove-item到您的链接,并将此脚本添加到您的页面。干净整洁。

<script>
$(function () {
  $('a.remove-item').click(function () {
    var url = this.href;
    $('#myModal .btn-danger').click(function () {
      window.location.href = url;
    });
  });
});
</script>
于 2013-09-22T10:13:24.323 回答
2

在您的引导标记中添加数据属性并向服务器发送 ajax 请求,因此可以在不刷新页面的情况下删除项目。

<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Confirm Delete ?</h3>
  </div>
<div class="modal-body">
  <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to Delete the Category and all its associated Properties?</p>
  </div>
  <div class="modal-footer">
    <a class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a>
    <a class="btn btn-danger" data-action="delete-item" data-item="XXX" data-dismiss="modal">Delete</a>
  </div>
</div>

现在使用 jquery 或 javascript,您可以向服务器发送请求以执行data-item我指定的删除操作XXX

如果您通过javascript方法生成数据项,动态添加数据项将很容易 -

function show_dialog_for(item_id)
{
   //your code here to generate and show the model
   //Bind the ajax method to send the delete request for the item_id
   $("a[data-action]").bind( "click", function() {
      //Send request for deletion
   });
}

现在您必须使用该链接的单击事件来绑定它,可以很容易地完成 -

$(".remove-item").bind( "click", function() {
   show_dialog_for($this.prop("data-item"));
});

对于以下链接 - 我在其中添加了一个类属性“remove-item”和带有 XXX 的数据项

<a href="www.mysite.com/product/1" class="remove-item" data-item="XXX" data-target="#myModal"  data-toggle="modal"><i class="icon-remove"></i> Delete</a>

希望这会帮助你。

于 2013-09-22T09:52:41.307 回答
2

工作演示在这里

您需要将此 javascript 代码添加到您的页面:

    $(".delBtn").click(function() {
        $("#delConfirmBtn").attr("href", $(this).attr("href"));
        $("#delConfirm").modal('toggle');
        return false;
    });

这是页面链接:

<a href="del_products.html?idproduct=1" class="delBtn btn btn-default">Delete</a>

这是模态链接:

<a href="#" id="delConfirmBtn" class="btn btn-danger">Delete</a>

This is the modal code:

  <!-- Modal -->
  <div class="modal fade" id="delConfirm" 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">Delete product</h4>
        </div>
        <div class="modal-body">
            <p>Lorem ipsum.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <a href="#" id="delConfirmBtn" class="btn btn-danger"><i class="icon-trash"></i> Delete</a>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

You can see your code on JSFiddle

于 2013-09-22T10:29:56.223 回答