first a little bit of documentation from the jQuery validation plugin:
"Use submitHandler to process something and then using the default submit. Note that "form" refers to a DOM element, this way the validation isn't triggered again."
submitHandler: function(form) {
$.ajax({
type: 'POST',
url: form.action,
data: 'current_password=' + form.current_password + '&new_password=' + form.new_password,
success: function(){
alert("succes");
}
});
}
So, naturally my ingenious piece of code isn't working. I'm trying to access the 'action' attribute and the two input fields from the form object, with no luck. How am I supposed to do this?
first a little bit of documentation from the jQuery validation plugin:
"Use submitHandler to process something and then using the default submit. Note that "form" refers to a DOM element, this way the validation isn't triggered again."
submitHandler: function(form) {
$.ajax({
type: 'POST',
url: form.action,
data: 'current_password=' + form.current_password + '&new_password=' + form.new_password,
success: function(){
alert("succes");
}
});
}
So, naturally my ingenious piece of code isn't working. I'm trying to access the 'action' attribute and the two input fields from the form object, with no luck. How am I supposed to do this?
Share Improve this question asked Aug 19, 2010 at 21:44 soren.qvistsoren.qvist 7,43615 gold badges65 silver badges93 bronze badges 2- What error do you get when running this? – Nick Craver Commented Aug 19, 2010 at 21:48
- Well if I change form.action to $(form).attr('action'), and let the data be, then it submits the following: current_password=[object HTMLInputElement]&new_password=[object HTMLInputElement] in firebug – soren.qvist Commented Aug 19, 2010 at 21:52
1 Answer
Reset to default 8Try this instead:
submitHandler: function(form) {
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: { current_password : form.current_password.value,
new_password: form.new_password.value }
success: function(){
alert("succes");
}
});
}
Currently instead of submitting the values of the elements it's literally calling a toString on them, so just add .value
to get the values. Also we're passing data
as an object here so it gets encoded, otherwise if for example the password had a &
in it, the post wouldn't be correct.