实时显示剩余可以输入的文字数

复制代码
<div class="row">  
    <div class="col-md-8">
        <div>
            <textarea name="counttextarea" id="counttextarea" cols="45" rows="5" style="width: 400px;padding: 10px; margin:10px;"></textarea>
        </div>  
    <div>
        <span name="countchars" id="countchars">100</span> Characters Remaining. <span name="percent" id="percent"></span>
    </div>  
</div>
复制代码
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
复制代码
<script>
$(document).ready(function(){
    var totalChars         = 100;                                                     // Total characters allowed in textarea
    var countTextBox     = $('#counttextarea');                                       // Textarea input box
    var charsCountEl     = $('#countchars');                                // Remaining chars count will be displayed here
    charsCountEl.text(totalChars);                                                    // initial value of countchars element
    countTextBox.keyup(function() {                                                   // user releases a key on the keyboard
        var thisChars = this.value.replace(/{.*}/g, '').length;                       // get chars count in textarea
        var per = thisChars*100; 
        var value= (per / totalChars);                                                // total percent complete
        if(thisChars > totalChars){                                                   // if we have more chars than it should be
            var CharsToDel = (thisChars-totalChars);                                  // total extra chars to delete
            this.value = this.value.substring(0,this.value.length-CharsToDel);        // remove excess chars from textarea
        }else{
            charsCountEl.text( totalChars - thisChars );                              // count remaining chars
            $('#percent').text(value +'%');
        }
    });    
});
</script> 
复制代码

 

posted @ 2015-01-07 13:31  壁虎漫步.  阅读(360)  评论(0)    收藏  举报
编辑推荐:
· 独立开发,这条路可行吗?
· 我在厂里搞 wine 的日子
· 如何通过向量化技术比较两段文本是否相似?
· 35+程序员的转型之路:经济寒冬中的希望与策略
· JavaScript中如何遍历对象?
阅读排行:
· 独立开发,这条路可行吗?
· C#源生成器:让你的代码飞起来的黑科技
· Java简历、面试、试用期、转正
· Java开发AI项目,太爽了!LangChain4j保姆级教程
· 极大提高项目部署的生产力!分享一个半自动化的CICD实现方案
点击右上角即可分享
微信分享提示