带有jquery的textarea中的最大字符数 [英] Max characters in textarea with jquery
本文介绍了带有jquery的textarea中的最大字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,我有点坚持下一步做什么。这个想法是当你在文本区域输入文本时,计数器会告诉你剩下多少个字符。一旦你达到最大字符我想停止允许输入字符,或删除所有输入的字符,因此文本区域中只有10个字符。我知道我必须将代码放在 alert(LONG);
的位置,但我不太确定是什么。
I have the following code, and I'm kind of stuck on what to do next. The idea is when you enter text into a text area a counter tells you how many characters you have left. Once you get to the max characters I want to stop allowing characters to be entered, or delete all the characters that were entered so there are only 10 characters in the text area. I know I have to put the code where it says alert("LONG");
but I'm not quite sure what.
var maxLen = 10;
console.log("Start");
$('#send-txt').keyup(function(){
var Length = $("#send-txt").val().length;
var AmountLeft = maxLen - Length;
$('#txt-length-left').html(AmountLeft);
if(Length >= maxLen){
alert("LONG");
}
});
推荐答案
这就是它。任何超出字符限制的内容都将被删除。
Here it goes. Anything beyond character limit will be removed.
$('textarea').keypress(function(e) {
var tval = $('textarea').val(),
tlength = tval.length,
set = 10,
remain = parseInt(set - tlength);
$('p').text(remain);
if (remain <= 0 && e.which !== 0 && e.charCode !== 0) {
$('textarea').val((tval).substring(0, tlength - 1));
return false;
}
})
这篇关于带有jquery的textarea中的最大字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文