Jquery - 使用enter键提交表单

Kar*_*k V 0 html jquery

如何使用Enter键提交表单.没有提交按钮?

我测试了下面的代码,但它不起作用.

$("input").bind("keydown", function(event) {
   console.log('came');
   var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
   if (keycode == 13) { // keycode for enter key
     document.getElementById('submitButton').click();
     // $('#frmAddPurchaseord').submit();
     return false;
     } else  {
     return true;
     }
    }); 
 <form name="frmAddPurchaseord" id="frmAddPurchaseord" action="" method="post">
   //Submit button
  <input id="submitButton" type="image" name="submit"  src="<?=$gCommonImagePath?>btn_save.png" />
 </form>
Run Code Online (Sandbox Code Playgroud)

请指教我!

Alp*_*ale 7

把事情简单化:

<form name="frmAddPurchaseord" id="frmAddPurchaseord" action="" method="post">
      <input type="text" name="name" onkeypress="if(event.keyCode==13) {this.submit();}" />
</form>
Run Code Online (Sandbox Code Playgroud)

使用jquery:

$("input").keypress(function(event) {
  if (event.which == 13) {
        event.preventDefault();
        $("#frmAddPurchaseord").submit();
    }
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.