简体   繁体   中英

Parsley.js validation with post

I'm validating a form with the (excellently crafted) Parsley.js script, and there's one element I can't seem to get working.

When the user successfully fills out the form, I want two things to happen:

  • The form posts to a website database.
  • A success message gets displayed.

So here's my code as it stands:

$(document).ready(function(){
    // form
    var form = $('#formname');
    var success = $('.success');

    form.submit(function(){

        if($form.parsley('isValid') == true){
            $.post('https://www.xxxxx.custom.api.url', $(this).serialize(), function(){
                success.show(); $('input[type=text]').val('');
            },'json');
        }

        return false;
    });

});

If I remove the "if" line and its corresponding bracket, my two commands work perfectly -- the form information gets submitted, and the message displays.

But when I include the "if" line and its bracket, it doesn't.

I'm not the world's biggest expert on jQuery/Javascript/etc., so I'm sure I'm missing something obvious. Any ideas what's wrong with this line?

if($form.parsley('isValid') == true){

Thanks, everyone!

You have to validate form and not $form .

$form is undefined in your example above.

Try if(form.parsley('isValid') == true){} and see if something good happens.

Best

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM