I am using Twitter bootstrap, I have specified a modal
<
-
For Bootstrap 3.1 you'll want to remove data and empty the modal-content
rather than the whole dialog (3.0) to avoid the flicker while waiting for remote content to load.
$(document).on("hidden.bs.modal", function (e) {
$(e.target).removeData("bs.modal").find(".modal-content").empty();
});
If you are using non-remote modals then the above code will, of course, remove their content once closed (bad). You may need to add something to those modals (like a .local-modal
class) so they aren't affected. Then modify the above code to:
$(document).on("hidden.bs.modal", ".modal:not(.local-modal)", function (e) {
$(e.target).removeData("bs.modal").find(".modal-content").empty();
});
讨论(0)
-
Expanded version of accepted answer by @merv. It also checks if modal being hidden is loaded from remote source and clears old content to prevent it from flashing.
$(document).on('hidden.bs.modal', '.modal', function () {
var modalData = $(this).data('bs.modal');
// Destroy modal if has remote source – don't want to destroy modals with static content.
if (modalData && modalData.options.remote) {
// Destroy component. Next time new component is created and loads fresh content
$(this).removeData('bs.modal');
// Also clear loaded content, otherwise it would flash before new one is loaded.
$(this).find(".modal-content").empty();
}
});
https://gist.github.com/WojtekKruszewski/ae86d7fb59879715ae1e6dd623f743c5
讨论(0)
-
$('#myModal').on('hidden.bs.modal', function () {
$(this).removeData('modal');
});
This one works for me.
讨论(0)
-
The problem is two-fold.
First, once a Modal object is instantiated, it is persistently attached to the element specified by data-target
and subsequent calls to show that modal will only call toggle()
on it, but will not update the values in the options
. So, even though the href
attributes are different on your different links, when the modal is toggled, the value for remote
is not getting updated. For most options, one can get around this by directly editing the object. For instance:
$('#myModal').data('bs.modal').options.remote = "http://website.com/item/7";
However, that won't work in this case, because...
Second, the Modal plugin is designed to load the remote resource in the constructor of the Modal object, which unfortunately means that even if a change is made to the options.remote
, it will never be reloaded.
A simple remedy is to destroy the Modal object before subsequent toggles. One option is to just destroy it after it finishes hiding:
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
Note: Adjust the selectors as needed. This is the most general.
Plunker
Or you could try coming up with a more complicated scheme to do something like check whether the link launching the modal is different from the previous one. If it is, destroy; if it isn't, then no need to reload.
讨论(0)
-
My project is built in Yii & uses the Bootstrap-Yii plugin, so this answer is only relevant if you're using Yii.
The above fix did work but only after the first time the modal was shown. The first time it came up empty. I think that's because after my initiation of the code Yii calls the hide function of the modal thereby clearing out my initiation variables.
I found that putting the removeData call immediately before the code that launched the modal did the trick. So my code is structured like this...
$ ("#myModal").removeData ('modal');
$ ('#myModal').modal ({remote : 'path_to_remote'});
讨论(0)
-
Good luck with this one:
$('#myModal').on('hidden.bs.modal', function () {
location.reload();
});
讨论(0)