最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using the jQuery validation plugin's submitHandler method properly - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

Try 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.

发布评论

评论列表(0)

  1. 暂无评论