2
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 when loading the page, if the return is null, I show a modal, if the result is not null, "drawing" a table using the function .html, so far so good, the problem is that when drawing the table, I draw also a button in the last cell and I say 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, there’s gonna be a alert showing a text, but this simple function is not working, apparently because it is running when from the click of a dynamically generated button, for test effect, I created a <button name="btn">btn teste</button> page html and function worked.
What I could do to function the alert?
Had chosen another answer as "correct", but this also worked and had an important mention to gain knowledge, since the link provided taught me things that I had not yet known. Thank you!
– D. Watson