How do I set the focus to the first input element in an HTML form independent from the id?

前端 未结 17 1870
遇见更好的自我
遇见更好的自我 2020-12-08 01:33

Is there a simple way to set the focus (input cursor) of a web page on the first input element (textbox, dropdownlist, ...) on loading the

17条回答
  •  粉色の甜心
    2020-12-08 02:14

    This gets the first of any visible common input, including textareas and select boxes. This also makes sure they aren't hidden, disabled or readonly. it also allows for a target div, which I use in my software (ie, first input inside of this form).

    $("input:visible:enabled:not([readonly]),textarea:visible:enabled:not([readonly]),select:visible:enabled:not([readonly])", 
        target).first().focus();
    

提交回复
热议问题