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

前端 未结 2 657
無奈伤痛
無奈伤痛 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

    0 讨论(0)
  • 2020-12-29 09:06

    Something a little different: maybe try setting your "PW-" just in text to the left of the text input field. Validate against non-numbers in the field and then when submitting the form, prepend the "PW-" to that field's value, so that you have a complete part number.

    0 讨论(0)
提交回复
热议问题