How to Select particular text in textbox while textbox receives focus

前端 未结 3 914
执笔经年
执笔经年 2021-01-23 00:28

I am developing one ASP.NET application with VB code.I have one textbox to hold the amount, which contains default value as \"0.00\". Now the problem is while the textbox gets f

相关标签:
3条回答
  • 2021-01-23 01:12

    assume you have TextInput you want to select its first character, whenever its selected

    <input id="MyText" type="text" value="text" onclick="MyText_click()" />
    

    and the script is like this:

    <script>
    
        function MyText_click() {
    
            var input = document.getElementById("MyText");
            createSelection(input, 0, 1); // first character
        };
    
        function createSelection(field, start, end) {
            if (field.createTextRange) {
                var selRange = field.createTextRange();
                selRange.collapse(true);
                selRange.moveStart('character', start);
                selRange.moveEnd('character', end);
                selRange.select();
                field.focus();
            } else if (field.setSelectionRange) {
                field.focus();
                field.setSelectionRange(start, end);
            } else if (typeof field.selectionStart != 'undefined') {
                field.selectionStart = start;
                field.selectionEnd = end;
                field.focus();
            }
        }
    
    </script>
    
    0 讨论(0)
  • 2021-01-23 01:17

    Create event for text focus

    Now we have two properties

       txt.SelectionStart = intValue
    

    and

      txt.selectionLength = length;
    

    Use this property to resolve your stuff

    0 讨论(0)
  • 2021-01-23 01:24

    you can do that programmatically using javascript, see this: Programmatically selecting partial text in an input field

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