5
I’m having trouble opening a modal by means of a button dynamically pressed via ajax.
The following jquery code below opens the modal normally, considering that the button to be clicked was not dynamically loaded.
$('#idDivModal5').on('click', '.btn-cancelar-contrato', function(){
// codigo aqui
});
Once I generated the buttons via ajax, sending them to my result div, the buttons no longer worked.
HTML code:
<div id=resultado></div>
Includes the result div in jquery as shown below, but it didn’t work:
$('#resultado#idDivModal5').on('click', '.btn-cancelar-contrato', function()
{
// codigo aqui
});
Below is the current code:
$('[data-load-remote]').on('click',function(e) {
e.preventDefault();
var $this = $(this);
var remote = $this.data('load-remote');
if(remote) {
$($this.data('remote-target')).load(remote);
}
});
Ajax request:
$.ajax({
type : "POST",
url : "envia-buscar-contrato",
data : {var_radio:var_radio,var_valor:var_valor},
datatype : 'html',
success: function(data) {
$("#form-buscar :input").prop("disabled", false);
$('.bloquear2').hide();
$('#resultado').show();
$("#resultado").slideDown();
$("#resultado").html(data);
}
});
});
Skeleton of the modal idDivModal5:
<div id="idDivModal5" class="modal fade">
<div class="modal-dialog modal-lg">
<div class="modal-content">
</div>
</div>
</div>
Button returned via ajax:
<div id="resultado">
<a class="botao-form" href='#idDivModal5' role='button'
data-toggle='modal'
data-load-remote='cancelar-contrato/<?php echo $codigo_contrato;?>'
data-remote-target='#idDivModal5 .modal-content'
aria-hidden='true'>Cancelar contrato</a>
</div>
Remember that when the button above is not pressed via ajax, it opens the idDivmodal5 modal normally. From the moment I return the button via ajax, it does not open. Where I am missing?
Put your source code please.
– Hugo Leonardo
I’ll post the code.
– Luis
the button needs to be pressed inside the
$(document)
, to make sure the snippet exists.– William Aparecido Brandino
What would Wilian look like?
– Luis
Thank you Wiliam. Replace $('[data-load-remote]'). on('click',Function(e) { by $(Document). on('click', '[data-load-remote]', Function(e) { , and worked =D.
– Luis