textarea 输入框倒数字数限制

老子叫甜甜 提交于 2020-01-18 09:28:38
<div class="wordCount" id="wordCount">
    <textarea placeholder="textarea还剩余字数统计"></textarea>
    <span class="wordwrap"><var class="word">50</var>/50</span>
</div>
$(function() {

    //先选出 textarea 和 统计字数 dom 节点
    var wordCount = $("#wordCount"),
        textArea = wordCount.find("textarea"),
        word = wordCount.find(".word");
    //调用
    statInputNum(textArea, word);

});
/*
 * 剩余字数统计
 * 注意 最大字数只需要在放数字的节点哪里直接写好即可 如:<var class="word">200</var>
 */
function statInputNum(textArea, numItem) {
    var max = numItem.text(),
        curLength;
    textArea[0].setAttribute("maxlength", max);
    curLength = textArea.val().length;
    numItem.text(max - curLength);
    textArea.on('input propertychange', function() {
        var _value = $(this).val().replace(/\n/gi, "");
        numItem.text(max - _value.length);
    });
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!