I have a form that has two buttons. One to save the record, and the other to cancel the save procedure. I use rails.js (a regular AJAX / jQuery plugin for those of you who donβt know) a javascript file that works with jQuery for unobtrusive javascript / ajax calls. When I submit form data via ajax, I want the name and value of the button I clicked to be sent along with the rest of the data so that I can decide what to do based on which button was clicked.
The method in the rails.js file uses .serializeArray() to send form data to the server. The problem is that this does not include the name / value pair of the button I pressed. The jQuery website says that they do it on purpose (although my opinion is that they should):
"The .serializeArray() method uses standard W3C rules for remote controls to determine which elements it should include, in particular the element cannot be disabled and must contain a name attribute. The submit button value is not serialized because the form was not submitted using the button. "
How can they suggest that the form was not submitted using the button? I suppose this makes no sense and erroneous assumptions.
In accordance with W3C rules, the button that was activated to submit the form is considered a successful control .
Since the jQuery developers decided to do this on purpose, can I assume that there is another method that DOES NOT NOT exclude the activated button in serialization?
EDIT: Here is a quick example of how my form will look ...
<!DOCTYPE html5> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $('#form').submit(function(e) { </script> </head> <body> <form id="form"> <input name="name" type="text"><br/> <input name="commit" type="submit" value="Save"/> <input name="commit" type="submit" value="Cancel"/> </form> </body>
jquery serialization w3c form-submit
DJTripleThreat 24 Oct '10 at 11:00 2010-10-24 11:00
source share