简体   繁体   中英

Bootstrap modal dialog button click event triggering on modal open

I am working on an application and need to open a modal , have the user fill in some details and save them once they click the modal button i have designated. I have used jQuery on to bind a click event for the button. problem is the click event is triggered when the modal opens.

Relevant parts of modal:

<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<a href="#" class="btn btn-primary" id="save-event">Save Event</a>

Where i bind the event:

$('#save-event').on(
'click',
function(evt)
{
    console.log('triggered');
}
);

The console shows 'triggered' when i open the dialog. I open the dialog through:

<a href="#add-event-modal" role="button" class="btn" data-toggle="modal">{% trans "Event also occurs on" %}</a>

Its a Django app so the curly braces. Any ideas what i could be doing wrong or is there another way to execute some logic once the user clicks the relevant button?

您可能在html中有2个具有相同ID的元素,并且在单击另一个元素时触发了事件。

Try adding the following to your jQuery code:

evt.stopPropagation();

Sorry, should clarify, add this to the code triggering the modal opening.

Edit: since you don't have direct access to the modal code, try the following in your jQuery:

$('#add-event-modal').on('click', function(evt) {
    evt.stopPropagation();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM