Therefore, I am trying to prevent the form from being submitted if the input is empty. The code looks something like this:
$(document).on("submit", "form#input_form", function(e){
if(!$(this).find("input[type=text]").val()){
e.preventDefault();
e.stopPropagation();
}
});
This does not work. The request simply passes despite the fact that it performs the preventDefault and stopPropagation functions.
However, if I do the following, it will successfully prevent sending:
$("form#input_form").on("submit", function(e){
if(!$(this).find("input[type=text]").val()){
e.preventDefault();
e.stopPropagation();
}
});
I don’t understand what the difference is, it’s basically the same thing, except that the former attaches a handler at the document level, and the latter to the form itself. By the way, I use form_foran assistant to render the form.
source
share