I noticed that the problem occurs when creating bootable modal code using jQuery. Adding additional javascript in a dynamically generated partial representation (my modal) causes a double background to appear. Does anyone know why this is happening? I am using jquery-1.8.2.js and Bootstrap v3.1.1. Many thanks.
App.js
$(document).ready(function () {
$('.modal-button').on('click', function (e) {
$('.modal').remove();
$.ajax({
url: '/Ajax/GetSearchModal/',
type: "get",
cache: false,
success: function (data) {
var modal = $(data);
$(data).modal({
backdrop: 'static',
keyboard: false,
});
}
});
});
});
Partial view (my modal)
@model MVCApp.Web.Models.ModalSearchModel
<div id="Ajax-Modal" class="modal fade" aria-hidden="true" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3>
Select Something
</h3>
</div>
@using (Html.BeginForm(Model.Action, Model.Controller, FormMethod.Post))
{
<div class="modal-body">
@Html.HiddenFor(m => m.SelectedDropDownID, new { @class = "select-something-ddl", style = "width:100%", placeholder = "Search..." })
</div>
<div class="modal-footer">
<button type="submit" value="Continue">Continue</button>
<button type="button" value="Cancel" data-dismiss="modal">Cancel</button>
</div>
}
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
});
</script>
UPDATE
Moving javascript from modal to success handler did not help achieve what I'm trying to achieve. I want to run select2 dropdown. The only way to make it work in the success handler is this:
modal.on('shown.bs.modal', function () {
$('#SelectedDropDownID').select2({
ajax: {
url: '/Ajax/FetchResults/',
dataType: 'json',
delay: 250,
data: function (searchTerm) {
return { query: searchTerm };
},
results: function (data) {
return { results: data };
}
},
minimumInputLength: 2,
formatResult: formatRepo,
formatSelection: formatRepoSelection
});
});
select2 , , . , select2 .
, cvrebert. Bootstrap jQuery, . , , select2 fixed select2, . :
function formatRepo(item) {
var markup = "<table class='item-result'><tr>";
if (item.text !== undefined) {
markup += "<div class='item-name' data-jtext=" + item.text + " data-jid=" + item.id + ">" + item.text + "</div>";
}
markup += "</td></tr></table>"
return markup;
}
function formatRepoSelection(item) {
return item.text;
}
$('.modal-button).on('click', function (e) {
// Delete existing modals.
$('.modal').replaceWith('');
// Get modal.
$.ajax({
url: '/Ajax/GetSearchModal/',
type: "get",
cache: false,
success: function (data) {
var modal = $(data);
$('body').append(modal);
$('
ajax: {
url: '/Ajax/FetchResults/',
dataType: 'json',
delay: 250,
data: function (searchTerm) {
return { query: searchTerm };
},
results: function (data) {
return { results: data };
}
},
minimumInputLength: 2,
formatResult: formatRepo,
formatSelection: formatRepoSelection
});
modal.modal({
backdrop: 'static',
keyboard: false,
});
}
});
});