Enter fires the form submit

前端 未结 2 893
不思量自难忘°
不思量自难忘° 2021-01-22 20:35

I have a div (nice gif displayed as-if it\'s a button), on that div is a piece of JavaScript that handles the ENTER as-if it\'s a click $(this).c

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-22 20:55

    This is a handy dandy jQuery function I use to get around this.

    $(formSelector).find('input, select').keypress(function (event) {
    
        if (event.keyCode == 13) {
            event.preventDefault();
            $(buttonSelector).click().focus();
    
            return false;
        }
    });
    

    formSelector is just a variable holding the #FormId, and buttonSelector is a variable holding the button I want to be clicked. so in your case, it would be: #IdOfYourDivToClick.

提交回复
热议问题