需要jquery或javascript来限制textarea中的每行长度

需要jquery或javascript来限制textarea中的每行长度,javascript,jquery,html,css,textarea,Javascript,Jquery,Html,Css,Textarea,我有以下html文本区域: <textarea name="splitRepComments" cols="20" rows="3" ></textarea> 我已经使用jQuery对其应用了maxlength限制。该功能如下: var max = 100; $('#splitRepComments').bind("keypress", function(e) { if (e.which < 0x20) { // e.which

我有以下html文本区域:

<textarea name="splitRepComments"   cols="20" rows="3" ></textarea>

我已经使用jQuery对其应用了maxlength限制。该功能如下:

var max = 100;
$('#splitRepComments').bind("keypress", function(e) {


    if (e.which < 0x20) {
        // e.which < 0x20, then it's not a printable character
        // e.which === 0 - Not a character
        return; // Do nothing
    }
    if (this.value.length == max) {
        e.preventDefault();
    } else if (this.value.length > max) {
        // Maximum exceeded
        this.value = this.value.substring(0, max);
    }
});

$('#splitRepComments').bind("paste", function(e) {


    setTimeout(function() {

        var e = jQuery.Event("keypress");
        e.which = 50; // # Some key code value
        $('#splitRepComments').trigger(e);
    }, 100);

});
var max=100;
$('#splitRepComments').bind(“按键”,函数(e){
如果(如<0x20){
//如果小于0x20,则它不是可打印字符
//e.which===0-不是字符
return;//什么也不做
}
if(this.value.length==max){
e、 预防默认值();
}else if(this.value.length>max){
//超过最大值
this.value=this.value.substring(0,最大值);
}
});
$('#splitRepComments').bind(“粘贴”,函数(e){
setTimeout(函数(){
var e=jQuery.Event(“keypress”);
e、 其中=50;/#一些关键代码值
$('#splitRepComments')。触发器(e);
}, 100);
});
我的障碍是我有一个要求,我们希望用户在每行(行)中只输入10个字符。然后输入下一行

此函数还应遵守textarea的maxlength限制

此外,我已经尝试了下面的解决方案,但并没有将输入带到下一行

我已经准备好供您参考。

试试这个:
try this:

    var max = 100;
    var characterPerLine = 10;

    $("textarea[name='splitRepComments']").bind("keypress", function(e) {

        if (e.which < 0x20) {
            // e.which < 0x20, then it's not a printable character
            // e.which === 0 - Not a character
            return; // Do nothing
        }
        //calculate length excluding newline character
        var length =  this.value.length - ((this.value.match(/\n/g)||[]).length);
        if (length == max) {
            e.preventDefault();
        } else if (length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
        }
        if (length % characterPerLine == 0 && length!=0 && length < max)                           {
            $(this).val($(this).val()+'\n');
        }

    });
var max=100; var characterPerLine=10; $(“textarea[name='splitRepComments'])。绑定(“按键”,函数(e){ 如果(如<0x20){ //如果小于0x20,则它不是可打印字符 //e.which===0-不是字符 return;//什么也不做 } //计算不包括换行符的长度 var length=this.value.length-((this.value.match(/\n/g)| |[]).length); 如果(长度==最大值){ e、 预防默认值(); }否则,如果(长度>最大值){ //超过最大值 this.value=this.value.substring(0,最大值); } 如果(长度%characterPerLine==0&&length!=0&&length
这里有一个纯JavaScript解决方案(您可以随意将其转换为JQuery),我尝试为每条语句添加注释:

var manageTextAreaSize = function manageTextAreaSize(fieldId, cols, rows){ 
    // Get textarea text value 
    var value = document.getElementById(fieldId).value; 
    // Save current cursor position (for mid text typing) 
    var currentPosition = document.getElementById(fieldId).selectionStart; 
    // Arraye will be used to save separate text lines 
    var lines = []; 
    // Remove newline characters (by splitting the text using newline character as separator then joining by blank) 
    // then cut off extra characters (for limited number of rows only) 
    value = value.split("\n").join(""); 
    if(rows){ 
      value = value.substr(0, cols * rows); 
    } 
    // If text length is superior to max number of characters (more than one line) 
    if(value.length > cols){ 
      // Split text by lines of max character length 
      for(var i = 0; i < value.length; i += cols){ 
        lines.push(value.substr(i, cols)); 
      } 
      // Reassemble text into the text are by joining separate lines 
      document.getElementById(fieldId).value = lines.join("\n"); 
      // Reposition cursor back to previously saved position (before resizing text) 
      document.getElementById(fieldId).selectionStart = document.getElementById(fieldId).selectionEnd = currentPosition; 
    } 
    // If text length is inferior to max number of characters (less than on line) 
    else if(value.length < cols){ 
      // Do nothing ! 
      return; 
    } 
    // If text length is equal to max number of characters (one line only) 
    else{ 
      // Go to next line 
      document.getElementById(fieldId).value += "\n"; 
    } 
  };
var managetextaresize=函数managetextaresize(fieldId、cols、rows){
//获取文本区域文本值
var值=document.getElementById(fieldId).value;
//保存当前光标位置(用于中间文本键入)
var currentPosition=document.getElementById(fieldId).selectionStart;
//Arraye将用于保存单独的文本行
var行=[];
//删除换行符(通过使用换行符作为分隔符拆分文本,然后通过空格连接)
//然后删除额外的字符(仅限行数)
value=value.split(“\n”).join(“”);
如果(行){
value=value.substr(0,列*行);
} 
//如果文本长度大于最大字符数(多行)
如果(value.length>cols){
//按最大字符长度的行拆分文本
对于(var i=0;i
用法:直接输入HTML事件(在下面的示例中:每行35个字符,最多6行)



注意:我没有在没有调整大小的情况下测试这个,所以在使用这个解决方案时要小心

当我设置max=20时;第一行我可以输入11和第二行9。你检查过这个了吗?
<textarea id="myTextArea" cols="70" rows="20" style="resize: none" oninput="manageTextAreaSize(this.id, 35, 6);"></textarea>