I have the following code:
var nReqAJAX = nReqAR
$.ajax({
type: "POST",
url: "../controller/ajax.selectItemRequisicaoPesquisar.php",
data: {'numeroRequisicao': nReqAJAX},
dataType: "JSON",
success: function(res) {
if(res == null)
{
$('#modalReqInvalida').modal('show');
}
else
{
var html = res.reduce(function(string, obj, i) {
return string + '<tr class="text-center"><td style="vertical-align: middle;">' + i + '</td><td style="vertical-align: middle;">' + obj.nome_GAThemocomponente + '</td><td style="vertical-align: middle;">' + obj.qtd_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.frequencia_GATitemRequisicao + '</td><td style="vertical-align: middle;">' + obj.cirurgia_GATitemRequisicao + '</td><td><button id="1" name="btnAtender" class="btn btn-success" style="width: 100%;">Atender Item</button></td></tr>'
}, '');
$("#tab_logic tbody").html(html);
}
}
});
$("button[name='btnAtender']").on(function(){
alert('teste');
});
A AJAX
is executed when loading the page, if the return is null, I show a modal, if the result is not null, "draw" a table using the .html
function, so far so good, the problem is that when I draw the table, I also draw a button
in the last cell and say that the name of this button
is btnAtender
, however, I would like to do the following function:
$("button[name='btnAtender']").click(function(){
alert('teste');
});
That is, when the user clicks on button
btnAtender
, a alert
will appear showing text, however this simple function is not working, apparently because it is executed when from the click of a generated button dynamically, for test purposes, I created a <button name="btn">btn teste</button>
in html page and the function worked.
What could I do with the alert
function?