Javascript 在文本区域输入密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:46:19  来源:igfitidea点击:

Enter key in textarea

javascripttextarea

提问by sat

I have a textarea, On every Enterkey pressed in textareaI want new line to be started with a bullet say (*). How to go about it ?

我有一个textarea, 在Enter按下的每个键上,textarea我希望新行以项目符号 (*) 开头。怎么办?

No jQuery please.

请不要使用jQuery。

I can observe for the Enterkey , after that !? Should I have to get the whole value of textareaand append * to it and again fill the textarea?

我可以观察Enter钥匙,然后!?我是否必须获取 的全部值textarea并将 * 附加到它并再次填充textarea?

回答by williamtroup

You could do something like this:

你可以这样做:

<body>

<textarea id="txtArea" onkeypress="onTestChange();"></textarea>

<script>
function onTestChange() {
    var key = window.event.keyCode;

    // If the user has pressed enter
    if (key === 13) {
        document.getElementById("txtArea").value = document.getElementById("txtArea").value + "\n*";
        return false;
    }
    else {
        return true;
    }
}
</script>

</body>

Although the new line character feed from pressing enter will still be there, but its a start to getting what you want.

虽然按回车键的换行符仍然存在,但它是获得你想要的东西的开始。

回答by Jean-Pierre Vaillancourt

Simply add this tad to your textarea.

只需将此一点添加到您的文本区域即可。

onkeydown="if(event.keyCode == 13) return false;"

回答by Tim Down

You need to consider the case where the user presses enter in the middle of the text, not just at the end. I'd suggest detecting the enter key in the keyupevent, as suggested, and use a regular expression to ensure the value is as you require:

您需要考虑用户在文本中间而不是最后按 Enter 的情况。我建议按照建议检测keyup事件中的回车键,并使用正则表达式来确保该值符合您的要求:

<textarea id="t" rows="4" cols="80"></textarea>
<script type="text/javascript">
    function formatTextArea(textArea) {
        textArea.value = textArea.value.replace(/(^|\r\n|\n)([^*]|$)/g, "*");
    }

    window.onload = function() {
        var textArea = document.getElementById("t");
        textArea.onkeyup = function(evt) {
            evt = evt || window.event;

            if (evt.keyCode == 13) {
                formatTextArea(this);
            }
        };
    };
</script>

回答by MuraliKrishna

My scenario is when the user strikes the enter key while typing in textarea i have to include a line break.I achieved this using the below code......Hope it may helps somebody......

我的情况是当用户在 textarea 中输入时敲击 Enter 键时,我必须包含一个换行符。我使用下面的代码实现了这一点......希望它可以帮助某人......

function CheckLength()
{
    var keyCode = event.keyCode
    if (keyCode == 13)
    {
        document.getElementById('ctl00_ContentPlaceHolder1_id_txt_Suggestions').value = document.getElementById('ctl00_ContentPlaceHolder1_id_txt_Suggestions').value + "\n<br>";
    }
}

回答by QuyPham

You could do something like this:

你可以这样做:

$("#txtArea").on("keypress",function(e) {
    var key = e.keyCode;

    // If the user has pressed enter
    if (key == 13) {
        document.getElementById("txtArea").value =document.getElementById("txtArea").value + "\n";
        return false;
    }
    else {
        return true;
    }
});
<textarea id="txtArea"></textarea>