转:jQuery控制input的字数

HTML内容:

<input class="form-control" id="title" name="title" placeholder=" 请输入" type="text">

<span id="titleCount" style="color:#9B9B9B;text-decoration: none;font-size:12px;">还可以输入12个字</span>

  

JS内容:

$("#titleCount").text("还可以输入" + (12 - $("#title").val().length) + "个字");
$("#title").keyup(function() {
    if ($("#title").val().length > 12) {
        $("#title").val($("#title").val().substring(0, 12));
    }
    $("#titleCount").text("还可以输入" + (12 - $("#title").val().length) + "个字");
}); 

当然,要引入jquery文件

 

转载自:http://www.jq22.com/webqd4097

posted @ 2019-07-01 11:22  傅丹辰cds  阅读(262)  评论(0编辑  收藏  举报