在光标处插入文本框中的文本

问题描述:

我在用户输入文本的aspx页面中有一个文本框。现在当用户点击一个名为“Sin”的按钮时,文本框应该显示“Sin []”并且光标必须放在括号之间。如下所示:“Sin [< cursor here>]现在,当用户单击在其他某个按钮上说“Cos”文本框文本应该显示“Sin [Cos []]”,并且光标位于Cos的括号之间,如下所示:“Sin [Cos [ cursor here>]]”。在光标处插入文本框中的文本

这是如何处理的。任何简单的代码,请..

在此先感谢

+0

“输入助手”,我想。 – 2010-06-04 07:15:37

我希望你正在使用jQuery。

<asp:TextBox id="txt" runat="server" /> 
<input type="button" id="sinBtn" value="Sin" tag="Sin[<cursor here>]" /> 
<input type="button" id="cosBtn" value="Cos" tag="Cos[<cursor here>]" /> 
//also you can generate the two buttons with server controls 

这里是JavaScript:

$(document).ready(function(){ 
    $('input[tag]').click(function(){ 
     var theText = $('#<%= txt.ClientID %>'); 
     theText.txt(theText.txt().replace('<cursor here>',this.tag); 
    }) 
}); 

如果你没有一个方法与string.replace使用原型扩展之一。

+0

嗨 感谢您的回复。您提供的代码不起作用。你可以描述一下吗? – Anil 2010-06-04 07:54:59