2013-05-22 28 views
0

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

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

我该如何做到这一点?

+0

你想在客户端或服务器端实现? – Dolo

+0

我想在客户端实现它。 – sniggy

+0

http://stackoverflow.com/a/3870087/706456,http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged.aspx,http:// stackoverflow .com/a/6153068/706456 – oleksii

回答

0
function format(input) { 

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


    } 

使用JavaScript功能上面,并呼吁在你的文本像这样onchange="format(this);"

+0

我试过这样的东西,但它不起作用。这里是代码: 私人代码txtBox_TextChanged(对象发件人,EventArgs e){txt = txtBox.Text; txtBox.Text =“(”+ txt +“)”; txtBox.ForeColor = System.Drawing.ColorTranslator.FromHtml(“#FF0000”); – sniggy

0

它的工作我 -

你的文本将是─

<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> 
相关问题