而文本框接收焦点如何在文本框中选择特定文本 [英] How to Select particular text in textbox while textbox receives focus

查看:143
本文介绍了而文本框接收焦点如何在文本框中选择特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发用VB code.I 1 ASP.NET应用程序中有一个文本框持有量,其中包含默认值0.00。现在的问题是,而文本框获得焦点时,它选择的所有文本。但我只想选择0即前precision,我不希望以后precision选择。

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 focus it selects all the text. But i want to select only 0 that is before precision and I do not want to select after precision.

希望有人能帮助我。在此先感谢

Hope someone will help me. Thanks in advance

推荐答案

假设你有的TextInput 您要选择的第一个字符,只要它选择了

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

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

和剧本是这样的:

<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>

这篇关于而文本框接收焦点如何在文本框中选择特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆