Milano Slesarik Milano Slesarik - 2 months ago 20
Javascript Question

JQuery submit - infinite loop

I'm trying to disable all elements which has style

display:none
before submitting the form.

If there is
e.preventDefault()
, the form is not submitted at all and if there is no
e.preventDefault()
the infinite loop occures.

$(document).on('submit', 'form', function (e) {
e.preventDefault();
console.log($(':input:hidden').length);
$('#reservation-form > :input:hidden').attr("disabled", true);
$('#reservation-form').unbind('submit').submit();

});


Do you know what to do to make all
display:none
fields disabled before submitting this form?

Answer

You don't have to run $('#reservation-form').unbind('submit').submit();

Remove the e.preventDefault(); call and return true at the end of the function:

$(document).on('submit', 'form', function (e) {
    console.log($(':input:hidden').length);
    $('#reservation-form > :input:hidden').attr("disabled", true);
    return true;
});

This should give you the behavior you want without having to bind and unbind events.

Comments