3
Good morning, I have a table and in each row of this table I have a button, when I click this button I need to run a function but I’m not able to do it because I think I don’t know how to use the correct selectors.
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 I want to run at 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!")
}
})
But where is the html for
#pesquisar
,.corpoTbl
,#tbl .btn
..? Please check out the guide How to create a Minimum, Complete and Verifiable example– brasofilo
@brasofilo these elements are created dynamically, with Javascript. It is in the question script.
– sergiopereira