[英]How can I truncate the contents of a textarea using JavaScript?
I have a text field, I am using onkeypress to check the length of the entered string. 我有一个文本字段,我正在使用onkeypress来检查输入字符串的长度。 If it exceeds n number of characters it will return false and it will not accept any other character, But Here I am unable to remove the characters I don't want in thetext field.
如果它超过n个字符,它将返回false,并且将不接受任何其他字符,但是在这里,我无法在文本字段中删除不需要的字符。 How can I remove the text here.
如何在此处删除文本。
Try this, if you are using textarea
如果您正在使用
textarea
,请尝试此操作
<textarea onkeypress="return limitlength(this, 20)" style="width: 300px; height: 90px"></textarea>
function limitlength(obj, length){
var maxlength=length
if (obj.value.length>maxlength)
obj.value=obj.value.substring(0, maxlength)
}
Check the length of the entered text in Onkeyup event. 检查Onkeyup事件中输入文本的长度。 If it is greter than n then use substring to truncate first n characters and then set the new string as the value
如果比n大,则使用子字符串截断前n个字符,然后将新字符串设置为值
$("#mytxt").onkeyup(function(){
var Currentlength =$(this).val().length;
var CurrentVal = $(this).val();
if(Currentlength > n)
{
$(this).val(CurrentVal.substring(0,n));
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.