Button Click event fires when pressing Enter key in different input (no forms)

后端 未结 6 2103
离开以前
离开以前 2020-12-12 21:42

This logic is firing when pressing \"Enter\" in the \"Amount\" input, and I don\'t believe it should (it doesn\'t in Chrome). How can I prevent this, and if not prevent it

6条回答
  •  情深已故
    2020-12-12 21:57

    I ran into this problem today. IE assumes that if you press the enter key in any of the text fields, you want to submit the form -- even if the fields are not part of a form and even if the button is not of type "submit".

    You must override IE's default behavior with preventDefault(). In your jQuery selector, put in the div that contains the text boxes you want to ignore the enter key -- in your case, the "page" div. Instead of selecting the whole div, you could also specify the text boxes you want to ignore specifically.

    $('#page').keypress(function(e) {
        if(e.which == 13) { // Checks for the enter key
            e.preventDefault(); // Stops IE from triggering the button to be clicked
        }
    });
    

提交回复
热议问题