Reputation: 1
I am working on an HTML-/PHP-/Jquery-App and got some problems with the .each-return-Value of jQuery.
I've got a form that has several input-fieldsets and you can add another fieldset if you need:
<input id="plot1_id" type="hidden" value="2">
<input id="plot1_position" type="hidden" value="1">
<textarea id="plot1_txt" class="plot_entries">Entry number 1</textarea>
<input id="plot2_id" value="12">
<input id="plot2_position" type="hidden" value="2">
<textarea id="plot2_txt" class="plot_entries">Entry number 2</textarea>
<input id="plot3_id" value="545">
<input id="plot3_position" type="hidden" value="3">
<textarea id="plot3_txt" class="plot_entries">Entry number 3</textarea>
[etc.]
When saved - jQuery uses
i=1;
$('.plot_entries').each( function() {
fieldsetAR.push( fieldsets(
$('#plot' + i + '_id').val(),
$('#plot' + i + '_position').val(),
$('#plot' + i + '_txt').val()
) );
i=i+1;
});
to get every input-field seperated to 'save' to an array and push through ajax's .load().
My Problem: I can't get the last entry right! The last or newly added input-element won't be registered as the last object within the .each-function.
For example: The last entry returned by the script would be
plot2_id = '2'
plot2_txt = 'Entry number 2'
instead of the real last one (plot3_id & plot3_txt)...
I'm totally confused - hopefully someone can help me out :/
E: Still confused and not solved :( Any other idea?
Upvotes: 0
Views: 218
Reputation: 5731
Use jQuery's index argument instead of preserving the number yourself.
$('.plot_entries').each( function(i) {
++i;
fieldsetAR.push( fieldsets(
$('#plot' + i + '_id').val(),
$('#plot' + i + '_position').val(),
$('#plot' + i + '_txt').val()
));
});
Upvotes: 1
Reputation: 25527
Probably because your i
starts from 0. Change it to start with 1
var i=1;
$('.plot_entries').each( function() {
fieldsetAR.push( fieldsets(
$('#plot' + i + '_id').val(),
$('#plot' + i + '_position').val(),
$('#plot' + i + '_txt').val()
) );
i=i+1;
});
Upvotes: 1