I have a form that dynamically includes rows. In this form I have an autocomplete field where I select the product and add the value of the product in another input. My problem is this ... if I include two rows and select the product the value doubles in the price field (according to the image below) I would like each included row to take the value of that product. Follow the autocomplete code and include lines:
function autoCompletar() {
$('.produtoDesc').autocomplete({
source: function (request, response) {
$.ajax({
url: '../models/retornaProduto.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'produto'
},
success: function (data) {
response($.map(data, function (item) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data: item
}
}));
}
});
},
minLength: 0,
select: function (event, ui) {
var names = ui.item.data.split("|");
$("input[name='produtoValor[]']").val(names[1]);
}
});
};
function adicionaLinha() {
var addLinha = $("#qtdLinhas")["val"]();
let novoCampo;
let idLinha;
for (var i = 0; i < addLinha; i++) {
novoCampo = $("tr.linhas:first")["clone"]();
idLinha = parseInt($("tr.linhas:last").prop("id").split("item_")[1]) + 1;
novoCampo.find('input').val("");
novoCampo.insertAfter("tr.linhas:last").attr("id", "item_" + idLinha).find("span").html(idLinha);
removeLinha();
autoCompletar();
}
;
};