简体   繁体   中英

bootstrap modal closes when ok on alert() is clicked

Bootstrap modal is closing when I click on "Ok" button showing from "alert()" function.

$("#expample").modal({
    backdrop: 'static',
    keyboard: false,
    show: true
});

Below is my jquery:

$('.sendToUser').on('click', function (){
            var selectedLyrics = $('#selectedLyricsInput').val();
            if($("input[name='selectUserRadio']").is(":checked") == false){
                alert('Please, select a user to send.');
                return;
            }
        });

Although, hitting "Enter" or "Esc" on keyboard is working fine and also, clicking outside the modal; the modal is not closing which is fine.

Below is my html:

<div class="modal fade" id="userSelectionModal" tabindex="-1" role="dialog" aria-labelledby="userSelectionModal">
    <div class="modal-dialog modal-likes" role="document">
        <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">Select User</h4>
            </div>

            <div class="modal-body">
                <div style="margin-bottom: 50px;" id="populateUsers">

                </div>
                <input type="hidden" id="selectedLyricsInput" value="" />
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-success sendToUser" data-dismiss="modal">Send</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
            {{--<i class="fa fa-spinner fa-spin"></i>--}}
        </div>
    </div>
</div>

I am creating an alert popup from an event on the modal but when clicked on the "ok" of the alert() popup, it disappears.

How to prevent closing of the modal after clicking on "OK" on the alert popup?

Thanks

Your modal is closing, because you have data-dismiss="modal" attribute on the .sendToUser button. Removing this attribute should prevent the modal from closing.

You need to remove attribute data-dismiss="modal" at the .sendToUser element.
If you need to close modal, you just need to add the code:

$('#userSelectionModal').modal('hide')

use within your script

window.location = "..\path after validation"

also avoid data-dismiss="modal" in submit button

Simply instantiate a variable that's tied to the events firing within the modal window - to false , further set the variable to false when deemed necessary (eg to prevent closing the modal when the alert pops up), and then finally when the window should normally close, do a quick "if check" that assesses if the variable is finally true and if it is, fire off the functionality that lets the modal close. This ensures that the modal window will close only when you allow it.

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