We have a web page with 2 forms that contain multiple fields. The first form has a customer billing address, and the second form has payment information. When a customer presses the Enter key, the billing address form is always submitted, regardless of whether the field that had focus was in the second payment information form.
The page needs to recognize the user's context, so that if a field in the second form has focus, and that person presses the Enter key, then that second form should submit, instead of the first form.
I am familiar with detecting the keypress event, and have used scripts like this to do so:
detectEnterKey: function () {
$('body').live('keyup', function (e) {
if (e.keyCode == 13) {// 'enter' key
// Do something
}
});
$('form').keypress(function (e) {
if (e.which === 13) {
// Do something
}
});
},
What I don't know how to do, is detect the form context to know which form to submit. I suspect it may be available in the event (function(e)), but haven't seen an example of this. Thanks.
You find the form that is parent of the field that has the focus when pressing enter and submit it.
$('.input').keyup(function(e) {
if(e.keyCode == 13) {
$(this).parent().find('.form').submit();
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments