简体   繁体   中英

How can I execute js code on enter key form submit without form.submit?

I have a third party form to track users on the page. I need to track all page submits on the page including submit fails due to js validations. Broad structure is as following:

<form>
//Elements
</form>
<a id="btnSubmit"></a>

I do not have control over structure of the form. To track the form submits I am using anchor tag click event: $('#btnSubmit').click(function () { //my code }); I am not using form.submit as the js code used in form validation prevents form.submit event from being called. It works well with click event except when user submits form with enter key. How can I track enter key form submit without form.submit?

You could try listening for the enter key being pressed on one of the input fields, or whatever suits in your form

$('input').keypress(function(e) {
        if(e.which == 13) {
            //your submit code
        }
    });

instead of click you can do is use the event "submit" for it.

Please look at the link https://api.jquery.com/submit/ for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM