Reputation: 55
I have a page with multiple forms on it each containing one submit button. When the button is pressed, I need to use the data from various hidden elements on the form.
Passing the data via Ajax is working for me using code like this...
$("body").on("submit", "form", function (event) {
var pm = $(this).serialize();
event.preventDefault();
$.post(
'urota3.php?' + pm
)
.....
The data for the correct form is passed to urota3 and all is well.
BUT...
I also need to use that data in the event function and I'm not sure how to get at it. I had tried adding a data section like this....
$("body").on("submit", "form", {
id: $('input:hidden[name=id]').val()
}, function (event) {...
but of course that picks up the data from the first form on the pade, not the one whose button you pressed.
How can I reference a piece of data from the correct form?
Thanks
Steve
Upvotes: 2
Views: 485
Reputation: 1665
You can use $(this) within the event function and search for your input from there ...
Upvotes: 1
Reputation: 21086
$("body").on("submit", "form", function (event) {
var pm = $(this).serialize();
pm[id] = $(this).find('input:hidden[name='+id+']').val();
event.preventDefault();
$.post(
'urota3.php?' + pm
)
Upvotes: 2