4
我有一個自舉按鈕(I使用自舉4)打開一個模式並傳輸數據,在按鈕在由函數模態形式:呼叫show.bs.modal事件父事件之前
$('#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">×</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>
@GuruprasadRao日Thnx爲編輯:) –
是我的榮幸.. :) –
感謝的答案 但我的問題是:在我的網站中,當我點擊父母作品時,我需要父母中的所有其他孩子。 並且在您的解決方案中,它不適用於任何小孩。 當所有其他孩子點擊時,我需要父母作品的功能。 看看我的[示例codepen](https://codepen.io/esty_sh/pen/qjjZee)。 非常感謝 – Esty