Move focus to a particular field

后端 未结 3 629
臣服心动
臣服心动 2021-01-17 17:57

I have a button that will add show a form on the page. how can I move the focus to the first field of the form when that button is clicked?

simple example:

H

相关标签:
3条回答
  • 2021-01-17 18:37

    It might be this:

    $("#newForm input:first").focus();
    
    0 讨论(0)
  • 2021-01-17 18:39

    Try this:

    $('input#firstfield').focus();
    
    0 讨论(0)
  • 2021-01-17 18:57

    A quicker lookup would simply be.

    $('#firstField').focus()
    

    However if you removed the ID from your element then this would be better but slightly slower.

    $('#newForm input:first').focus();
    
    0 讨论(0)
提交回复
热议问题