0
I have a button where I can add fields to my form, and I need each new field to have a different name, I thought of using name = "products[]", and so create an array and use a foreach on the back, for example. Can you help me with this concatenation? I’ve tried a few times and I couldn’t.
This is the function responsible for cloning the fields. the idea was to make name="products[cont]" and in others as well, how to do? I know that in this way, whenever I call the function, the value of cont will again be 1, suggestions of how to keep cont constant and how to Zera it after sending the form, please.
$(function () {
var srcDiv = $('#dinamicDiv');
var cont = 1;
$(document).on('click', '#addInput', function(){
$('<p>'+
'<label for="Produto" id="Produto" class="col-form-label">Nome do produto: </label>' +
'<button id="removeInput" class="btn-danger btn-add">Remover produto</button>' +
'<input type="text" class="form-control" name="produtos[]">' +
'<label for="Preço" id="Preço" class="col-form-label">Preço do produto:</label>' +
'<input type="text" class="form-control" name="preços[]">' +
'</p>').appendTo(srcDiv);
cont ++;
return false;
});
I put it on Jsfiddle to make it better understood: https://jsfiddle.net/Jaozim/5xj0tbrp/3/
– João
Zeroing this is done alone by the code itself if you do not store it in a Cookie for example, and when the sum of your var contains, you should use it like this: cont = cont++;
– ElvisP