2
I have the following code:
var contador = 1;
$('.preview-add-button').click(function(){
var form_data = {};
//form_data["concept"] = $('.payment-form input[name="concept"]').val();
//form_data["description"] = $('.payment-form input[name="description"]').val();
form_data["status"] = $('.payment-form #status option:selected').text();
form_data["amount"] = parseFloat($('.payment-form input[name="amount[]"]').val()).toFixed(2);
//form_data["date"] = $('.payment-form input[name="date"]').val();
form_data["remove-row"] = '<span class="ico-cancel"></span>';
var row = $('<tr></tr>');
$.each(form_data, function( type, value ) {
$('<td class="input-'+type+'"></td>').html(value).appendTo(row);
$('<input/>').attr("type", "hidden").attr("name", "produto[]").val(form_data["status"]).appendTo(row);
$('<input/>').attr("type", "hidden").attr("name", "produto[]").val(form_data["amount"]).appendTo(row);
contador++;
});
$('.preview-table > tbody:last').append(row);
calc_total();
});
The problem is in the counter variable, instead of counting from 1 to 1 as it is in the code, when passing through the loop it was sum 3 instead of 1, that is, it arrives in the loop worth 1 and in the next iteration it is worth 4, in the next 7 and so on, someone can understand why?
form_data has 3 elements, it is normal to add to the counter 3 times, you are inside a foreach. which means it will run that code 3 times
– Kup
And how do I fix it?
– Eduardo Paludo
What do you want the counter for? You cannot simply use a "form_data.length"?
– Kup
I got it, I just took the counter and the inputs from the haha loop, sorry about the Noob question
– Eduardo Paludo
Is that I need to send the data that were selected to PHP and I wanted to separate, product1=>array()...etc.. and since it was inside the loop, it sent the data the triplicate PHP from there, you know?
– Eduardo Paludo