简体   繁体   English

在textarea上使用onkeyup和onkeydown来控制最大长度

[英]onkeyup and onkeydown on textarea to control maxlength

I want to have a maxlength in textarea but html:textarea doesnt have the maxlength attribute. 我想在textarea中有一个maxlength,但是html:textarea没有maxlength属性。 So I used the foll. 所以我用了foll。 code: 码:

<html:textarea rows="5" cols="60" onkeyup="return imposeMaxLength(this, 50);" onkeydown="return imposeMaxLength(this, 50);" property="abc"></html:textarea>

function imposeMaxLength(Object, MaxLen)
    {
      return (Object.value.length <= MaxLen);
    }

This restricts the characters in the textarea to be < 50 but what happens is once you reach the limit(ie 50 in my case) the cursor is not functional. 这将文本区域中的字符限制为<50,但是一旦达到限制(在我的情况下为50),光标将无法工作。 I cannot move cursor in the textarea. 我无法在文本区域中移动光标。 I mean i cannot delete the content or use backspace or do a ctrl+A and delete. 我的意思是我无法删除内容或使用退格键或ctrl + A并删除。 The content remains unaltered or the textares content couldnt be changed. 内容保持不变,或者textares内容无法更改。 Is there any workaround to make it work or anyother suggestion? 是否有任何变通办法可以使它正常工作或有其他建议?

The problem with you approach it that you restrict any input after max length is reached. 您遇到的问题是在达到最大长度后限制任何输入。 Might be better to slice the input to the size you want. 最好将输入切成所需的大小。 Adding the event handlers is not necessary, but inline handlers in the html should be avoided. 不需要添加事件处理程序,但是应避免在html中使用内联处理程序。

var myTextarea = document.querySelector("#my-textarea");
var DEFAULT_MAX_LENGTH = 5;

function limitLength() {
  var maxLength = this.getAttribute("max-length") || DEFAULT_MAX_LENGTH;
  if (this.value.length > maxLength) {
    this.value = this.value.slice(0, maxLength);
  }
}

myTextarea.addEventListener("keydown", limitLength.bind(myTextarea));
myTextarea.addEventListener("keyup", limitLength.bind(myTextarea));

Example Code on Plunker 柱塞上的示例代码

I'm not too sure about your HTML syntax and floating JavaScript, but maybe that's just in your question. 我不太确定您的HTML语法和浮动JavaScript,但这也许只是您的问题。

In any event, at this point you cancel all keys once you reached the maximum length. 无论如何,在达到最大长度后,此时您将取消所有键。 What you want is leave the control keys alone. 您只需要保留控制键即可。 That includes the arrow keys, Ctrl-A, delete, backspace... 其中包括箭头键,Ctrl-A,删除,退格键...

In your test, check the key first and if it is a special key, then always return true. 在测试中,请首先检查该密钥,如果它是特殊密钥,则始终返回true。 Otherwise return the result of your current test. 否则,返回当前测试的结果。 You can test with an alert() or a console.log() to see what the special key codes are: 您可以使用alert()或console.log()进行测试,以查看特殊键码是什么:

console.log("Received key = " + Object.keycode);

The problem you'll have here also is that you're not using jQuery and this the "keycode" parameter is not going to work in all browsers. 您在这里遇到的问题还在于您没有使用jQuery,并且此“ keycode”参数在所有浏览器中都无法使用。 With jQuery you'd cancel the default propagation instead and use the event object the jQuery provides. 使用jQuery时,您可以取消默认传播,而使用jQuery提供的事件对象。 It makes your code a lot easier/cleaner to handle such things. 它使您的代码更容易/更轻松地处理这些事情。

PS I would avoid using the name "Object" for JavaScript variables. PS我将避免为JavaScript变量使用名称“ Object”。 "Obj" would probably be safer. “ Obj”可能会更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM