3
Hey, how you doing? I have a form with text 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 there will be in the form, since there may have been addition of new fields as well as deletion. However, when sending 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 is executed in the fomulário’s Submit (where I will already have all the form finished). Each field has the class .conteudoA
and in Submit I change the value of the attribute name
according to the value v
which 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 there are. If there are 3 fields:
name='A-3'
name='A-3'
name='A-3'
And I need you to stay:
name='A-1'
name='A-2'
name='A-3'
I’m using the .each()
wrong way or my logic doesn’t make sense? Can someone please help me?
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 to remove:
$('#formA').on('click', '.btnRemoveOpcaoA', function () {
$(this).parents('.conteudoA').remove();
});
Your logic goes like this: in the first iteration it’s all
name='A-1'
in the second iterationname='A-2'
and finally in the thirdname='A-3'
. That’s because everyone has the same class.– user60252
Gosh, friend, I didn’t pay attention to this detail. Thank you very much. However, there is some way to do this enumeration in the elements?
– Leandro Chaves
Gee, buddy, without the add and delete code it’s hard to work out any answers
– user60252
I edited the post with the information
– Leandro Chaves