Send form to keyCode == "enter" (13)

I need to submit the contents of the form when I press a key Enter, but only if the form does not have an error message. I created the following function:

$(targetFormID).submit(function (e) {
    var mess = error_m(targetDiv);
    if (e.keyCode == 13 && mess.length > 0) {
        e.preventDefault();
        e.stopPropagation();
    }
    if (mess.length == 0 && e.keyCode == 13) $(targetFormID).submit();
}); 

In this function, the messy variable receives the error message returned by the function error_m, the rest is a simple conformation of the code, but it does not work.

Need help with this!

+5
source share
1 answer

Submitting a form when a key is pressed Enteris the default browser behavior. Do not get involved in this. Just confirm the form in the event submit.

$(targetFormID).submit(function (e) {
    var mess = error_m(targetDiv);
    if (mess.length > 0) {
        e.preventDefault();
    }
});

: targetFormID? , ,

$("#" + targetFormID).submit(/* Same function as above */);

, $(targetFormID) , .

+7

All Articles