Jquery focus on first text field and set cursor at value end for instant data entry

前端 未结 2 674
無奈伤痛
無奈伤痛 2020-12-29 08:27

I am loading a form and defaulting focus to the first text field. The text field is populated with a default value \"PW-\". (The first characters of the customers part numbe

2条回答
  •  暖寄归人
    2020-12-29 09:05

    Here's an EXAMPLE:

    $(document).ready(function(){
        var el = $("input:text").get(0);
        var elemLen = el.value.length;
    
        el.selectionStart = elemLen;
        el.selectionEnd = elemLen;
        el.focus();
    });​
    

    https://developer.mozilla.org/en-US/docs/XUL/Property/selectionStart

    https://developer.mozilla.org/en-US/docs/XUL/Property/selectionEnd

    For older versions of IE you may need to use: http://msdn.microsoft.com/en-us/library/ie/ms536401(v=vs.85).aspx

提交回复
热议问题