时间:2018-05-13 18:12:41

标签: javascript jquery html bootstrap-modal jquery-events

如果用户点击表格行,我想打开生成的链接。在这一行,我有一个删除按钮。如果单击删除按钮,我想打开模态。

每次点击删除按钮,表格行也会被点击。

我找到了this但是模态不起作用。 我发现this,但每次都有我的目标。

为什么它不起作用?我怎么能解决这个问题?

function show_clothing(t_row) {

  var link = $(t_row).attr('data-href');
  console.log("Redirect to : " + link);

}

function delete_clothing(btn) {

  var clothing_id = $(btn).attr('data-clothing_id');
  console.log("Delete : " + clothing_id);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<table class="table table-light">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Button</th>
    </tr>
  </thead>
  <tbody>
    <tr onclick="show_clothing(this)" data-href="a_link">
      <td>#34</td>
      <td>
        <button data-toggle="modal" data-target="#deleteModalCenter" class="btn btn-danger btn-sm" data-clothing_id="34" onclick="delete_clothing(this)">Delete</button>
      </td>
    </tr>
  </tbody>
</table>




<div class="modal fade" id="deleteModalCenter" tabindex="-1" role="dialog" aria-labelledby="deleteModalCenterTitel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Delete clothing</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
          <p id="modal_text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam </p>
          <p id="modal_text_small">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam </p>
      </div>
      <div class="modal-footer">
         <a><button type="button" class="btn btn-secondary" data-dismiss="modal">chancel</button></a>
        <a id="delete_btn" href="/delete_person/"><button type="button" class="btn btn-danger">delete</button></a>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

event.cancleBuble = true,其他人在点击您的按钮时停止跟踪事件。这意味着如果您想在单击按钮后阻止单击行,则必须使用它。 你应该在cancleubble之后立即从javascript添加模态的初始打开。

function delete_clothing(btn) {

var clothing_id = $(btn).attr('data-clothing_id');
console.log("Delete : " + clothing_id);
event.cancelBubble=true;
$("#deleteModalCenter").modal('show');

}