在父事件之前调用show.bs.modal事件

时间:2017-06-21 11:42:36

标签: javascript jquery bootstrap-modal bootstrap-4 stoppropagation

我有一个引导按钮(我使用bootstrap 4)打开一个模态并通过函数将按钮中的数据传输到模态中的表单:

$('#exampleModal').on('show.bs.modal', function (event)

按钮位于div(父级)中。 当我点击按钮时,父事件在孩子面前调用。 我不希望运行父事件。 我只需要打开弹出模式。 当我尝试首先打开按钮打开按钮,但数据不是传输。 这是我的代码示例:codepen

这是html

<div class="row">
  <div class="col-12 parent">
    I'm the parent!
    <div class="child">
      I'm the child
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"       data-whatever="@mdo" data-message="@test message" >Open modal </button>

    </div>
  </div>
</div>



<!-- Bootstrap Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="form-control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="form-control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>

这是脚本

<script>
$(".parent").click(function(event){
  alert("Parent event");
})


/*
if I add this function I cant transfer the data that in the button
$(".child").click(function(event){
  alert("Child event");
  event.stopPropagation();
  $('#exampleModal').modal('show');
})*/



$('#exampleModal').on('show.bs.modal', function (event) {
  alert("btn event");
  var button = $(event.relatedTarget) // Button triggered the modal
  var recipient = button.data('whatever')
  var message = button.data('message') 
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
  modal.find('.modal-body textarea').val(message)
})
</script>

1 个答案:

答案 0 :(得分:3)

非常简单,在父点击的情况下,检查target是否与buttondata-target='#exampleModal'不同,所有使用.is() jQuery函数:< / p>

//get reference to the button 
var $button = $("button[data-target='#exampleModal']");
$(".parent").click(function(event){
  if (!$(event.target).is($button)) { // be sur button was'nt clicked
    alert("Parent event");
  }
})

<强> Updated Pen

见下面的工作片段:

var $button = $("button[data-target='#exampleModal']");

$(".parent").click(function(event){
  if (!$(event.target).is($button)) {
    alert("Parent event");
  }
})



$('#exampleModal').on('show.bs.modal', function (event) {
  alert("btn event");
  var button = $(event.relatedTarget) // Button triggered the modal
  var recipient = button.data('whatever')
  var message = button.data('message') 
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
  modal.find('.modal-body textarea').val(message)
})
body {
  background-color: #a3d5d3;
}

.col-12{ background:red; height:100px;}
.child{background:white;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
  <div class="col-12 parent">
    I'm the parent!
    <div class="child">
      I'm the child
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"       data-whatever="@mdo" data-message="@test message" >Open modal </button>

    </div>
  </div>
</div>



<!-- Bootstrap Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New message</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="form-control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="form-control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>