[英]Jquery : Put line break (press enter button) on entering certain character in text area
$('.mydesc').keyup(function () {
var len = $(this).val().length;
if(len == 63){
//put line break here
}
});
on reaching 63 characters including space the cursor should b moved to next line (effect when i get enter button) 在达到63个字符(包括空格)时,光标应b移动到下一行(当我进入Enter按钮时效果)
how can i do it in jquery ? 我该怎么做在jQuery中?
You can add line break using \\n
when reach 63
characters: 达到
63
字符时,可以使用\\n
添加换行符:
$('.mydesc').keyup(function () {
var len = $(this).val().length;
if (len == 63) {
this.value += '\n';
}
});
do something like this: 做这样的事情:
if(len == 63){
$(this).val($(this).val() + "\n");
}
You probably want something like this: 您可能想要这样的东西:
$('.mydesc').keyup(function () {
var $this = $(this);
var val = $this.val();
var lines = val.split('\n');
var result = [];
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
while (line.length > 5) {
result.push(line.substring(0, 5));
line = line.slice(5);
}
result.push(line);
}
$this.val(result.join('\n'));
});
Just change the 5's to the length you want. 只需将5更改为所需的长度即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.