简体   繁体   中英

Form not submitted after a second call

I try to submit a form if a condition is not validated.

    <form action="http://www.google.com" method="post" id="support_form">
<p class="accept"><input type="checkbox" id="id_cgu" name="cgu" value="1"> I accept
<input type="submit" value="ok">
</form>

<script language="javascript">

$("#support_form").submit(function() {
    if($("#id_cgu:checked").length==0) {
        alert("{% trans "Vous must accept terms of services" %}");
        return false;
    } else {
        alert('valid')
        return true;
    }
});

</script>

If my form is valid on the first call, my form is well submitted (redirection to another page)

If my form is not valid the first time, and I correct it then, I see the alert box "valid", but my form is not submitted.

Do you have an idea about this problem ?

It's because you returned false . You should instead use stopImmediatePropagation(); , like this:

$("#support_form").submit(function(ev) {
if($("#id_cgu:checked").length==0) {
    alert("{% trans "Vous must accept terms of services" %}");
    ev.stopImmediatePropagation();
} //rest of code...

When you return false, you essentially make that submit button useless until you reload the page.

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