我尝试在给定的javascript中为html-textarea添加char限制器。此外,我想添加一个剩余计数器。但我的代码无效。
你有一个想法,我错了吗?
maxLen=640;
if(text.length>maxLen)
{document.getElementById("message").value=text.substring(0,maxLen);}
else
{document.getElementsByClassName("totalcounter").value=maxLen-text.length;}
Demo on jsfiddle - >我的问题是第62-70行。
答案 0 :(得分:1)
你尝试过textarea的属性maxlength吗?
<textarea maxlength="50">
答案 1 :(得分:0)
让代码正常工作,移动if if else阻止&#34;返回&#34;(如下所示)并使用document.getElementsByClassName("totalcounter")[0].innerHTML=maxLen-text.length;
代替document.getElementsByClassName("totalcounter").value=maxLen-text.length;
maxLen=640;
if(text.length>maxLen)
{
document.getElementById("message").value=text.substring(0,maxLen);
}
else
{
document.getElementsByClassName("totalcounter")[0].innerHTML=maxLen-text.length;
}
return count = {
encoding: encoding,
length: length,
per_message: per_message,
remaining: remaining,
messages: messages
};
答案 2 :(得分:0)
我的解决方案如下:
限制5条消息后的短信(在手机上显示为1)并添加剩余的总计数
<!-- maxLen GSM7: 765 chars = 5 Messages
<!-- maxLen GSM7EX: 765 chars - (number of used "gsm7bitExChar"-Chars * 2)= 5 Messages
<!-- maxLen UTF16: 335 chars = 5 Messages
if(encoding == 'GSM_7BIT'){
maxLen=764;
if(text.length>maxLen)
{
document.getElementById("message").value=text.substring(0,maxLen);
}
else
{
document.getElementsByClassName("totalcounter")[0].innerHTML=maxLen-text.length;
}
return count = {
encoding: encoding,
length: length,
per_message: per_message,
remaining: remaining,
messages: messages
}
}; <!-- close if encoding 1
if(encoding == 'GSM_7BIT_EX'){
maxLen=762;
if(text.length>maxLen)
{
document.getElementById("message").value=text.substring(0,maxLen);
}
else
{
document.getElementsByClassName("totalcounter")[0].innerHTML=maxLen-text.length;
}
return count = {
encoding: encoding,
length: length,
per_message: per_message,
remaining: remaining,
messages: messages
}
};<!-- close if encoding 2
if(encoding == 'UTF16'){
maxLen=334;
if(text.length>maxLen)
{
document.getElementById("message").value=text.substring(0,maxLen);
}
else
{
document.getElementsByClassName("totalcounter")[0].innerHTML=maxLen-text.length;
}
return count = {
encoding: encoding,
length: length,
per_message: per_message,
remaining: remaining,
messages: messages
}
};<!-- close if encoding 3
希望这也有助于其他人。的 Solution on jsfiddle 强>
特别感谢danxexe