Form not submitted after the second call

I am trying to submit a form if the condition is not verified.

    <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 presented (redirect to another page)

If my form is invalid for the first time, and then I correct it, I see a "valid" warning window, but my form is not submitted.

Do you have an idea about this problem?

+3
source share
1 answer

This is because you returned false. Instead, you should use stopImmediatePropagation();, for example:

$("#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 actually make this submit button useless until you reload the page.

0
source

Source: https://habr.com/ru/post/1763575/


All Articles