Frage

before a submit-action is fired i´m doing a little check before:

$(document).ready(function(){
$("#submit").submit(function(e){

e.preventDefault();

var check = $("#check").val();

        if(check == 1) {
            $("#submit").submit();
        } else {
            alert"input-hidden value is wrong")
        }

}

});
});

So i am checking if a value of a input-hidden-value is set to 1.

If it is, the form action should be processed, but instead i get this error:

too much recursion

anybody could help with this?

EDIT:

In this line:

$("#submit").submit();

this part:

#submit

is underlined, why?

War es hilfreich?

Lösung

It is because calling $("#submit").submit(); again triggers the submit event which will again invoke the submit handler thus creating an infinite recursive loop... call the dom elements submit method instead like this.submit()

or use the preventDefault() only if the value is invalid else allow the default action to continue

$(document).ready(function () {
    $("#submit").submit(function (e) {
        var check = $("#check").val();
        if (check != 1) {
            e.preventDefault();
            alert("input-hidden value is wrong")
        }
    });
});

Andere Tipps

You shouldn't call $("#submit").submit() to allow submit to happen, this causes your handler to be called over and over. Instead, you should move preventDefault() after the error alert() and return true instead of calling submit().

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top