美文网首页
2018-09-13textarea字数限定

2018-09-13textarea字数限定

作者: Sunshinelyp | 来源:发表于2018-09-13 17:19 被阅读0次

HTML

<textarea class="wishContent" placeholder="请输入不超过15个字" maxlength="15"> </textarea>

<span class="wordsNum">0/15</span>

Script

var checkStrLengths = function (str, maxLength) {

        var maxLength = maxLength;

        var result = 0;

        if (str && str.length > maxLength) {

            result = maxLength;

            return result;

        } else {

            result = str.length;

        }

        return result;

}

//监听输入

$(".wishContent").on('input propertychange', function () {

    //获取输入内容

    var userDesc = $(this).val();

    //判断字数

    var len;

    if (userDesc) {

        len = checkStrLengths(userDesc, 15);

    } else {

        len = 0

    }

    //显示字数

    $(".wordsNum").html(len + '/15');

});

相关文章

网友评论

      本文标题:2018-09-13textarea字数限定

      本文链接:https://www.haomeiwen.com/subject/ueqggftx.html