Trigger a button click with JavaScript on the Enter key in a text box

后端 未结 30 1917
难免孤独
难免孤独 2020-11-21 23:53

I have one text input and one button (see below). How can I use JavaScript to trigger the button\'s click event when the Enter key is pressed ins

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:43

    Although, I'm pretty sure that as long as there is only one field in the form and one submit button, hitting enter should submit the form, even if there is another form on the page.

    You can then capture the form onsubmit with js and do whatever validation or callbacks you want.

提交回复
热议问题