Prevent users from submitting a form by hitting Enter

后端 未结 30 3538
感动是毒
感动是毒 2020-11-21 05:13

I have a survey on a website, and there seems to be some issues with the users hitting enter (I don\'t know why) and accidentally submitting the survey (form) without clicki

30条回答
  •  醉酒成梦
    2020-11-21 05:41

    In my specific case I had to stop ENTER from submitting the form and also simulate the clicking of the submit button. This is because the submit button had a click handler on it because we were within a modal window (inherited old code). In any case here's my combo solutions for this case.

        $('input,select').keypress(function(event) {
            // detect ENTER key
            if (event.keyCode == 13) {
                // simulate submit button click
                $("#btn-submit").click();
                // stop form from submitting via ENTER key press
                event.preventDefault ? event.preventDefault() : event.returnValue = false;
            }
        });
    

    This use case is specifically useful for people working with IE8.

提交回复
热议问题