我在asp.net中有多行文本框,有7行,最大大小为700字节。现在,如果超过它,每行应该只占用100个字节,应该移到下一行,直到达到最大大小。
http://jsfiddle.net/3e3EH/721/
下面代码有效:
<textarea id="foo" cols="50" var rows="7"></textarea>
$(document).ready(function(){
var textArea = $('#foo');
var maxRows = textArea.attr('rows');
var maxChars = textArea.attr('cols');
textArea.keypress(function(e){
var text = textArea.val();
var lines = text.split('\n');
if (e.keyCode == 13){
return lines.length < maxRows;
}
else{ //Should check for backspace/del/etc.
var caret = textArea.get(0).selectionStart;
console.log(caret);
var line = 0;
var charCount = 0;
$.each(lines, function(i,e){
charCount += e.length;
if (caret <= charCount){
line = i;
return false;
}
//\n count for 1 char;
charCount += 1;
});
var theLine = lines[line];
return theLine.length < maxChars;
}
});
});