문제

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?

도움이 되었습니까?

해결책

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")
        }
    });
});

다른 팁

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().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top