Twitter-like Textbox Character Count with inline alert

若如初见. 提交于 2019-12-06 05:54:04

问题


I would like to have a textbox character "count up" that will increase the count as a user types and display a text alert when the user crosses the required character but still allows user to continue typing.


回答1:


If you want to roll your own

HTML

<div>
  Type text here:
  <input type="text" id="txt" />
</div>
<div id="warning" style="color: red; display: none">
  Sorry, too much text.
</div>
<br>
<input type="text" id="counter" disabled="disabled" value="0"/>

Script

$("#txt").keyup(function () {
    var i = $("#txt").val().length;
    $("#counter").val(i);
    if (i > 10) {
        $("#warning").show()
    } else {
        $("#warning").hide()
    }
});

JSFiddle here




回答2:


You can check this plugins http://plugins.jquery.com/plugin-tags/character-counter



来源:https://stackoverflow.com/questions/7529792/twitter-like-textbox-character-count-with-inline-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!