当我按 escape 时如何按 space

How to press space when i press escape

在下面的例子中,当我按下回车按钮时,我该如何执行 "space bar pressed" 操作?
我想要的是当我按下 enter 时,它会添加一个 space,就像我按下 space 栏时一样。

var textarea = document.getElementById("textarea");

textarea.onkeypress = function(){return (event.charCode >= 48 && event.charCode <= 57)||(event.charCode == 32);};
textarea{
  height : 100px;
  width : 500px;
}
<textarea id="textarea" placeholder="Here you can only press numbers and space bar !"></textarea>

将此添加到您的代码中:

if(event.charCode == 13){
        textarea.value = textarea.value + " ";
}

完整代码:

var textarea = document.getElementById("textarea");

textarea.onkeypress = function (event) {
    if(event.charCode == 13){
        textarea.value = textarea.value + " ";
    }
    return (event.charCode >= 48 && event.charCode <= 57) || (event.charCode == 32);
};

Here is the JSFiddle demo

var textarea = document.getElementById("textarea");

textarea.onkeypress = function(event){
  var code = (event.keyCode ? event.keyCode : event.which);
  if(!(code == 13 || code == 32 || code == 8 || code == 46 || (code>= 48 && code <= 57))){
    event.preventDefault();
   
  }
 
};
textarea{
  height : 100px;
  width : 500px;
}
<textarea id="textarea" placeholder="Here you can only press numbers and space bar !"></textarea>