【问题标题】:Adding bracket when key in a number输入数字时添加括号
【发布时间】:2013-05-17 02:52:58
【问题描述】:

假设我有一个文本框A。当应用程序运行时,将显示 textboxA 并且用户可以输入一个数字。我想将用户输入格式化为红色字体并用括号括起来。

示例:如果用户输入“5”,我会将输入格式化为 (5),颜色为红色。

我如何做到这一点?

【问题讨论】:

标签: c# asp.net numbers brackets


【解决方案1】:
function format(input) {

        var txtValue = document.getElementById(input).value;
        document.getElementById(input).value = "("+txtValue+ ")";
document.getElementById(input).style="color:red;";


    }

使用上面的javascript函数并像这样在你的文本框中调用onchange="format(this);"

【讨论】:

  • 我已经尝试过类似的方法,但它不起作用。代码如下: 私有代码 txtBox_TextChanged(object sender, EventArgs e){ String txt = txtBox.Text; txtBox.Text = "(" + txt + ")"; txtBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
【解决方案2】:

它对我有用-

您的文本框将是-

<asp:TextBox ID="TextBox1" runat="server" onchange="javascript:formatText(this);"></asp:TextBox>

在客户端编写如下函数-

<script type="text/javascript" language="javascript">
    function formatText(input) {
        var txtValue = document.getElementById(input.id).value;
        document.getElementById(input.id).style = "color:Red;";
        document.getElementById(input.id).value = "(" + txtValue + ")";        
    }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多