I'm having trouble opening a modal via a dynamically loaded button via ajax.
The following jquery code below opens the modal normally, assuming the button to be clicked was not loaded dynamically.
$('#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>
Include the div result in jquery, as shown below, but did not work:
$('#resultado#idDivModal5').on('click', '.btn-cancelar-contrato', function()
{
// codigo aqui
});
The following is the code below:
$('[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);
}
});
});
Modal skeleton 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>
Remembering that when the above button is not loaded via ajax, it usually opens the modal idDivmodal5. From the moment I return the button via ajax, it does not open. Where am I going wrong?