当用户按Enter键时在文本框中添加* [英] Adding * in textbox as user press enter

查看:71
本文介绍了当用户按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 textbox

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


这篇关于当用户按Enter键时在文本框中添加*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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