How to submit a form when the return key is pressed?

前端 未结 15 1034
既然无缘
既然无缘 2020-11-28 04:25

Can someone please tell me how to submit an HTML form when the return key is pressed and if there are no buttons in the form? The submit button is not there

15条回答
  •  孤城傲影
    2020-11-28 05:04

    To submit the form when the enter key is pressed create a javascript function along these lines.

    function checkSubmit(e) {
       if(e && e.keyCode == 13) {
          document.forms[0].submit();
       }
    }
    

    Then add the event to whatever scope you need eg on the div tag:

    This is also the default behaviour of Internet Explorer 7 anyway though (probably earlier versions as well).

提交回复
热议问题