I am wondering how to set the default button in html page, so that when enter key is pressed, the same effect of button pressed? For example, when we use Google, after entering query string in the middle input box, we can press enter key to get query results (i.e. press enter has the same effect of click "Google Search" button).
Any solutions? I am not sure whether there are different solutions when it is an html button, and when it is t开发者_如何学Pythonhe submit button of a html form?
thanks in advance, George
That's simply done with a <form>
element.
For example, if you have something like this:
<form action="something.php" method="post">
<input type="text" name="blah">
<input type="submit">
</form>
and you press enter in the input field, the form will be posted to something.php, ie. the same as if you press the submit button.
As long as your form is contained inside a <form>
tag and you have an input or button of type="submit"
, it should submit when you hit enter. If those two conditions aren't true, you can use javascript to detect onkeyup for the enter key.
精彩评论