I've got a form that's dynamically created using ajax (as data for the form elements has to come from a database) and I want to serialize the elements of the form to submit by ajax. I'm currently just testing my theory using code from the jQuery website just to see if I can pick up the form elements and this is where the problem lies:
$(document).ready(function() {
$('#btnCustomSearch').live('click', function() {
$('#results').html('');
alert($('#customSearchTable :input').serializeArray());
// get all the inputs into an array.
var fields = $('#customSearchTable :input').serializeArray();
jQuery.each(fields, function(i, field) {
$("#results").append(field.name + " = " + field.value + ", ");
});
// now we'll reformat the data as we need
// here we'll send the data via ajax
});
});
I need to make some changes to the data prior to submission and this code is not written yet, but what I'm finding is that any input elements on the page that existed at time of page loading are picked up correct, any elements that are populated using Javascript are picked up correctly, but any created using ajax are ignored.
I know this is normally resolved using "live", but I'm unclear as to how to resolve this with serializeArray()
. Using Ajax additional form elements are added to the #customSearchTable
and these are the ones not being picked up.
Any help great appreciated.
Thanks