简体   繁体   中英

jquery show/hide div after form validation

I show loading message after form submit like this:

<div id="loading" style="display:none;"><img class='image-widthauto' src="/img/Progressbar.gif" /></div>
<div id="content-alert"></div>
<form id="login" method="POST" action="" onsubmit="$('#loading').show();">
..... //input
</form>

i validate my form using jquery validation plugin:

$(document).ready(function () {
    $("#login").validate({
        errorElement: "div",
        errorPlacement: function(error, element) {
        error.appendTo("div#content-alert").addClass('alert alert-danger');
        }, 
        rules: {
            name: {
                required: true,
                minlength: 6,
                alpha: true,
            },
        },
        messages: {
            name: {
                required: "requied message",
                minlength: "6 character"
            },

        }
    });

});

Now, in form submit i see loading message and error box of validation(submit empty form value). i need to show loading message when my form not have a error using validation plugin.

how do fix this ?

Instead of using onsubmit attribute, use the submitHandler callback of the validator

$(document).ready(function () {
    $("#login").validate({
        errorElement: "div",
        errorPlacement: function (error, element) {
            error.appendTo("div#content-alert").addClass('alert alert-danger');
        },
        rules: {
            name: {
                required: true,
                minlength: 6,
                alpha: true,
            },
        },
        messages: {
            name: {
                required: "requied message",
                minlength: "6 character"
            },

        },
        submitHandler: function (form) {
            $('#loading').show();
            form.submit();
        }
    });

});

Try this : use .valid() method to decide whether to show loading message or not.

HTML :

<div id="loading" style="display:none;"><img class='image-widthauto' src="/img/Progressbar.gif" /></div>
<div id="content-alert"></div>
<form id="login" method="POST" action="" onsubmit="return showLoading();">
..... //input
</form>

jQuery ;

var form =  $("#login");

function showLoading()
{
  if(form.valid())
  {
   $('#loading').show();
   return true;
  }
  return false;
}

$(document).ready(function () {
    form.validate({
        errorElement: "div",
        errorPlacement: function(error, element) {
        error.appendTo("div#content-alert").addClass('alert alert-danger');
        }, 
        rules: {
            name: {
                required: true,
                minlength: 6,
                alpha: true,
            },
        },
        messages: {
            name: {
                required: "requied message",
                minlength: "6 character"
            },

        }
    });

});

From what I understand, you want to show the element with id loading when the form submits without an error.

The official documentation says :

Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it is validated.

So you could overwrite the submithandler like so:

Your html (just the form element, the rest could remain the same) :

 <form id="login" method="POST" action="#"> //Don't need the onsubmit here

Your javascript could be:

 $("#login").validate({
    errorElement: "div",
    errorPlacement: function(error, element) {
    error.appendTo("div#content-alert").addClass('alert alert-danger');
    }, 
    submitHandler: function(form) {
      $('#loading').show();   //Show the required element here
      form.submit(); //Submit the form normally if needed
    },
    rules: {
        name: {
            required: true,
            minlength: 6,
            alpha: true,
        },
    },
    messages: {
        name: {
            required: "requied message",
            minlength: "6 character"
        },

    }
});

Hope it gets you started in the right direction.

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