简体   繁体   中英

Twitter bootstrap modal shows and then disappears

I went through all possible questions related to Twitter Bootstrap modals but my problem is kind of unique over here.

I have included the files from the latest bootstrap release, so my public directory has -

css/
| bootstrap.css
| bootstrap.min.css

js/
| bootstrap.js
| bootstrap.min.js

I am also using Select2 and I have checked that it is not interfering with the bootstrap files.

My modals are defined this way -

<div class="modal hide fade" id="modal-<?php echo $user_role->id ?>" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3>Update &ldquo;{{ $user_role->role_name }}&rdquo;</h3>
    </div>
    <div class="modal-body">
        <label>New Value</label>
        <input type="hidden" name="inputId" value="<?php echo $user_role->id ?>">
        <input type="text" name="inputUpdate" value="<?php echo $user_role->role_name ?>" autofocus>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal" aria-hidden="true">Cancel</a>
        <a rel="update" href="<?php echo url('admin/dashboard/controls/update_master') ?>" class="btn btn-primary">Update</a>
    </div>
</div>

And to call the modal, the button is defined like this -

<a rel="edit" data-toggle="modal" href="#modal-<?php echo $user_role->id ?>" class="btn btn-info">Edit</a>

The JS that runs on the page is -

$(document).ready(function() {
    var urlModal;
    $('a[rel="edit"]').click(function() {
        urlModal = $(this).attr('href');
        $(urlModal).modal();
    });
});

Whenever I click on the <a rel="edit"> button, the modal appears and immediately goes away.

I just figured out the problem.

$('a[rel="edit"]').click(function() {
    urlModal = $(this).attr('href');
    $(urlModal).modal();
});

The $(urlModal).modal(); part wasn't required. The code in the HTML part of the button, itself shoots the modal. And this query was closing it.

Now, its working.

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