Submit form after unbinding

51 Views Asked by At
$("#qwerq").submit(function (e){
    e.preventDefault();
    var check=0;

    if($("#firstName").val() == "") {
        check=1;
    }

    if(check!=1){
        $("#qwerq").unbind("submit") ;
        $("#qwerq").submit();
        //$("#qwerq").trigger('submit', [true]);
    }
});

When the form is having id="qwerq" is as per needs and the submit gets unbinded, the form does not submit on its own.

I have tried using .submit() and .trigger("submit"). I have to manually click on submit again.

What should I add so that I don't have to click again?

2

There are 2 best solutions below

0
Robo Robok On BEST ANSWER

Instead of unbinding the events, why won't you just prevent submitting only on error?

if(check === 1) return false;

return false in jQuery's event handler means preventDefault and stopPropagation.

1
anand On

I think you were trying to submit a form on a button click. Then you need to make some changes in your code:

Provide an id to your form and change button type="button" (instead of "submit"):

<form id="form_1" action="yourserverpagename.php" method="post">
   <input id="firstName" type="text" value="" />
   <input id="qwerq" type="button" value="Send" />
</form>

Now your script should like below:

    <script>
            $("#qwerq").click(function (e) {
                e.preventDefault();

                var check = 0;

                if ($("#firstName").val() == "") {
                    check = 1;
                }

                if (check != 1) {
                    //-- here is the way to submit the form using form id --
                    $('#form_1').submit();
                }

            });
    </script>