Hello, I have a table and in each row of this table I have a button, when I click that button I need to execute a function but I can not do that because I do not think I'm using the correct selectors. p>
Table:
<table id="tbl" class="table table-striped">
<tr id="cabecalho">
<th>
ID
</th>
<th>
Insumo
</th>
<th>
TD
</th>
<th>
Unidade
</th>
<th>
Quantidade
</th>
<th>
Adicionar
</th>
</tr>
</table>
Script that generates the table body:
$('#pesquisar').click(function () {
$('.corpoTbl').remove();
$.ajax({
url: "/RCM/ListarMateriais",
type: "POST",
data: { nome: $('#NomeMaterial').val() },
datatype: 'json',
success: function (data) {
$.each(data, function (I, item) {
$('#tbl').append("<tr class=\"corpoTbl\"> <td class=\"id\">" + item.ID + "</td><td>" + item.Nome + "</td><td>" + item.TD + "</td><td>" + item.Unidade +
"</td><td> <input class=\"qtda\" type=\"text\" value=\"0\" style=\"width: 50px;\" /> </td><td> <input class=\"btn\" type=\"button\" value=\"Adicionar\" /> </td></tr>")
})
}
});
});
Script that I want to run on the click of the button.
$('#tbl .btn').click(function () {
var Qtd = $('.qtda');
var Id = $('.id');
if (Qtd.value != "0" && Qtd.value != "") {
$.ajax({
url: "/RCM/AddMaterial",
type: "POST",
data: { "Qtd": $('.qtda').Value, "Id": Id.textContent }
})
alert("Materiais Adicionados!");
}
else {
alert("Informe a quantidade do material!")
}
})