我在asp.net中有多行文本框,其中有7行,最大大小为700字节,现在,每行如果超过则应仅占用100字节,应移至下一行直到达到最大大小。 / p>
答案 0 :(得分:0)
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;
}
});
});