-->

How to submit a form OnKeyPress with Javascript?

2019-07-30 12:14发布

问题:

I want to make a form like this, and i want to post the form - with javascript - in all the keydowns.

<form action="{$formaction}" enctype="multipart/form-data" method="post">
<input type="text" name="n">
<input type="password" name="pw">
<button name="in" type="submit">enter</button>
</form>

please tell me how to do this.

回答1:

<form onkeydown="this.submit();">
  <!-- form content -->
</form>


回答2:

<body onkeydown="document.forms["myform"].submit();">


回答3:

If you do that, the page will reload, just as if you were clicking the submit button. What you probably want is to attach an onkeydown handler to the password field and submit key presses via AJAX.

For an example look at one of javascript auto-suggest libraries, e.g. AJAX Auto Suggest.