当用户按Enter键时在文本框中添加*
问题描述:
你好朋友,
我试图编写javascript函数,在其中检查用户是否已按Enter键.如果是这样,那么我想通过javascript在文本框中的新行中添加* here is my aspx code:
hello friends,
I was trying to write javascript function, where in I check whether user has pressed enter key. If so then through javascript i want to add * in new line in textboxhere is my aspx code:
<asp:TextBox TextMode="MultiLine" runat="server" Width="80%" Height="100px" ID="txtComments" onkeypress="appendText(event);" Text="* ">
</asp:TextBox>
Here is my javascript code:
Here is my javascript code:
function appendText(e) {
var charChode;
if (e && e.which) {
e = e;
charChode = e.which;
}
else {
e = event;
charChode = e.keyCode;
}
if (charChode == 13) {
var TheTextBox = document.getElementById("<%=txtComments.ClientID %>");
// var textValue = document.getElementById("<%=txtComments.ClientID %>").value;
// textValue = textValue.replace(/\n\r?/g, ''<br />* '');
TheTextBox.value = TheTextBox.value + "<br/> * ";
return true;
}
else {
return false;
}
简而言之:I want to create bulleted text box, as user press enter bullet should appear on next line
任何解决方案吗?
在此先感谢
In short:I want to create bulleted text box, as user press enter bullet should appear on next line
Any solution??
Thanks in advance
答
替换
TheTextBox.value = TheTextBox.value + "<br /> * ";
到
to
TheTextBox.value = TheTextBox.value + "\n * ";
这是我尝试并成功的方法
here is what i tried and was successful
function appendText(e) {
var charChode;
if (e && e.which) {
e = e;
charChode = e.which;
}
else {
e = event;
charChode = e.keyCode;
}
if (charChode == 13) {
var TheTextBox = document.getElementById("<%=txtComments.ClientID %>");
if (window.ActiveXObject) {
e.returnValue = false;
e.cancel = true;
TheTextBox.value = TheTextBox.value + "\n* ";
}
else {
e.preventDefault();
}
}
希望对您有帮助.
Hope it helps you.