Hello, how are you? I have a form with text type fields that can be added and deleted as the user likes. My question is this: when the user finishes filling the form I will not know how many fields will exist on the form, since there may have been addition of new fields as well as deletion. However, when submitting this form I need to number the fields in numerical order from 1 to the number of fields, because I will use this order later. Here's my Jquery and my logic:
var v = 1;
$('.conteudoA').each(function () {
$('input').prop("name", "A-" + v);
v++;
});
This Jquery runs on the submit form (where I'll already have the entire form completed). Each field has the class .conteudoA
and in the submit mute the value of the name
attribute according to the value v
that changes according to the iteration of .each()
.
It turns out that at the end of the iteration the fields are all getting the same value of name
according to the number of fields that exist. If there are 3 fields:
name='A-3'
name='A-3'
name='A-3'
I need you to stay:
name='A-1'
name='A-2'
name='A-3'
Am I using .each()
wrongly or does my logic not make sense? Can someone help me please?
Thanks!
EDIT: To add the fields use an append
$("#btnAddOpcaoA").click(function () {
$("<div class='row conteudoA'><div class='col-md-10'><input name='' type='text' class='form-control' opcao='A' ordem='' id='' placeholder=''/></div><div class='col-md-2'><button style='margin-top: 5px;' type='button' class='btnRemoveOpcaoA btn btn-danger btn-xs'><span class='glyphicon glyphicon-minus'></span></button></div></div>").appendTo('#formA');
});
And stop removing:
$('#formA').on('click', '.btnRemoveOpcaoA', function () {
$(this).parents('.conteudoA').remove();
});