【发布时间】:2014-12-12 08:18:34
【问题描述】:
<script type="text/javascript">
function checking(textarea, maxLines, maxChar) {
var lines = textarea.value.replace(/\r/g, '').split('\n'), lines_removed, char_removed, i;
if (maxLines && lines.length > maxLines) {
lines = lines.slice(0, maxLines);
lines_removed = 1
}
if (maxChar) {
i = lines.length;
while (i-- > 0) if (lines[i].length > maxChar) {
lines[i] = lines[i].slice(0, maxChar);
char_removed = 1
}
if (char_removed || lines_removed) {
textarea.value = lines.join('\n')
}
}
}
</script>
这是我根据之前的几篇文章使用的。但是这段代码允许我在“m”行中输入“n”个字符。有人可以帮我写一个只需要 30 个字符和 2 行的代码吗? {假设如果第一行包含 10 个字符,则下一行应包含 20 个字符,但总字符数应为 30,行数为 2。}
【问题讨论】:
-
你也可以发布你的 HTML 吗?和 var lines = textarea.value.replace(/\r/g, '').split('\n'), lines_removed, char_removed, i;似乎有一些函数调用,但你写的不正确
-
stackoverflow.com/questions/556767/… 基于这篇文章我得到了这个代码。 @Alok
-
另请注意,如果您没有对文本框长度进行服务器端检查,这可能涉及安全问题
标签: javascript html