简体   繁体   中英

How to trigger a submit action for an input element or form while pressing enter key?

Have a simple search bar (input field) which should trigger an onSubmit action while we press Enter button

You can use onKeyPress. Check this: https://stackoverflow.com/questions/13987300/how-to-capture-enter-key-press

Simple, have a button type submit inside the form. Here's an example:

const onSubmit = () => {}
// ..
<form onSubmit={onSubmit}>
  ...
  <button type="submit">Search</button>
  ...
</form>

You don't need to use onKeyPress event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM