没有输入的Javascript剩余字符

没有输入的Javascript剩余字符

本文介绍了没有输入的Javascript剩余字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有一个纯JavaScript文本剩余计数器,它输出< span> < p> code>标记而不是输入字段?我只能找到Jquery解决方案或在输入字段中输出的解决方案。

解决方案

< span> 。所以我只想分享给大家,希望它可以派上用场。
$ b HTML:

 < textarea id =messagecols =20rows =5name =messageonKeyDown =textCounter('message','messagecount ,100);的onkeyup =textCounter(消息, messagecount,100);>< / textarea的> 
< span id =charsleft>< / span>

Javascript:

 <脚本> 
函数textCounter(textarea,countdown,maxlimit){
var textareaid = document.getElementById(textarea);
if(textareaid.value.length> maxlimit)
textareaid.value = textareaid.value.substring(0,maxlimit);
else
document.getElementById('charsleft')。innerHTML ='('+(maxlimit-textareaid.value.length)+'characters available)';
}
< / script>

< script type =text / javascript>
textCounter('message','messagecount',100);
< / script>

这也是一个工作



注意:如果有人想为脚本做出贡献以使其更好,请随时这样做。我不是Javascript的专家,所以它很可能是一个更加用户友好的解决方案。



亲切的问候


Is it possible to have a pure Javascript text remaining counter that outputs the value in a <span> or <p> tag rather than an input field? I can only find Jquery solutions or ones that output in input fields.

解决方案

Have seen over the net that a lot of people are wanting a remaining characters counter that is pure Javascript and doesn't preview the number in an input box. I was messing around with JSFiddle lastnight and did a little work around and was able to get the remaining characters to show in other tags such as <span>. So I would just like to share this with everyone and hope it might come in handy.

HTML:

<textarea id="message" cols="20" rows="5" name="message" onKeyDown="textCounter('message','messagecount',100);" onKeyUp="textCounter('message','messagecount',100);"></textarea>
<span id="charsleft"></span>

Javascript:

<script>
    function textCounter(textarea, countdown, maxlimit) {
        var textareaid = document.getElementById(textarea);
        if (textareaid.value.length > maxlimit)
          textareaid.value = textareaid.value.substring(0, maxlimit);
        else
          document.getElementById('charsleft').innerHTML = '('+(maxlimit-textareaid.value.length)+' characters available)';
      }
</script>

<script type="text/javascript">
    textCounter('message','messagecount',100);
</script>

Here is also a working JSFiddle

Note: Should anyone want to contribute to the script to make it better, please feel free to do so. I am not an expert in Javascript so it most likely a more user friendly solution.

Kind Regards

这篇关于没有输入的Javascript剩余字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:46