Making Enter key on an HTML form submit instead of activating button

前端 未结 7 1587
北恋
北恋 2021-02-03 16:33

I have an HTML form with a single submit input, but also various button elements. When the user presses the \'enter\' key, I\'d expect it to actually s

7条回答
  •  温柔的废话
    2021-02-03 17:05

    Try this, if enter key was pressed you can capture it like this for example, I developed an answer the other day html button specify selected, see if this helps.

    Specify the forms name as for example yourFormName then you should be able to submit the form without having focus on the form.

    document.onkeypress = keyPress;
    
    function keyPress(e){
      var x = e || window.event;
      var key = (x.keyCode || x.which);
      if(key == 13 || key == 3){
       //  myFunc1();
       document.yourFormName.submit();
      }
    }
    

提交回复
热议问题