【发布时间】:2017-12-20 22:36:23
【问题描述】:
我有以下 html 文本区域:
<textarea name="splitRepComments" cols="20" rows="3" ></textarea>
我使用 jQuery 应用了最大长度限制。该函数如下:
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);
});
我的障碍是我有要求,我们希望用户在每行(行)中只输入 10 个字符。在该输入之后应该采取下一行。
这个函数也应该遵守 textarea 的 maxlength 限制。
此外,我已经尝试过 SO 的以下解决方案,但没有将输入输入到下一行。
https://stackoverflow.com/a/19876218/1487469
我已准备好jsfiddle 供您参考。
【问题讨论】:
标签: javascript jquery html css textarea