How to re-focus to a text field when focus is lost on a HTML form?

前端 未结 3 1758
梦谈多话
梦谈多话 2021-01-13 04:48

There is only one text field on a HTML form. Users input some text, press Enter, submit the form, and the form is reloaded. The main use is barcode reading. I use the follo

3条回答
  •  一个人的身影
    2021-01-13 05:29

    Darin's answer is right, but doesn't work in Firefox. If you want to steal focus back in Firefox too, you have to delay it until after the event:

    
    

    Or, better, assigned from JavaScript:

    
    

    But, I would strongly advise you not to do this. Clicking outside a text box to remove focus from that text box is perfectly normal browser behaviour, which can be of legitimate use (eg. to set search point for ctrl-F, or start a drag-selection, etc). There's very unlikely to be a good reason to mess with this expected behaviour.

提交回复
热议问题