我正在开发一个 JQuery 来显示在文本区域/输入字段中输入内容时剩余的字符数。但它似乎不起作用,我对 JQuery 很陌生,所以我确信下面的简单内容是代码的副本:
$(":textarea[data-limit-input]").keyup(function () {
var charLength = $(this).val().length;
var charLimit = $(this).attr("data-val-length-max");
// Displays count
$(this).next("span").html(charLength + " of " + charLimit + " characters used");
// Alert when max is reached
if ($(this).val().length > charLimit)
$(this).next("span").html("<strong>You may only have up to " + charLimit + " characters. </strong>");
});
如有任何帮助,我们将不胜感激
最佳答案
这对我有用:
我更改了文本区域选择器,向 charLimit 添加了正确的数据值,并关闭了 funktion 和 if 语句。
编辑 我更新了它,因此如果您在达到限制时尝试输入,它将返回 false 并使其更漂亮:)
关于用于显示文本区域/输入中剩余字符的 JQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8100921/