使用JavaScript显示HTML文本框中剩余的字符数
16lz
2021-01-22
This is my code:
这是我的代码:
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) {
field.value = field.value.substring(0, 160);
field.blur();
field.focus();
return false;
} else {
countfield.value = maxlimit - field.value.length;
}
}
How can I display how many characters are remaining from a certain text box with a limit of 160?
如何显示某个文本框中剩余的字符数限制为160?
10 个解决方案
#1
41
The code in here with a little bit of modification and simplification:
这里的代码有一点修改和简化:
<input disabled maxlength="3" size="3" value="10" id="counter">
<textarea onkeyup="textCounter(this,'counter',10);" id="message">
</textarea>
<script>
function textCounter(field,field2,maxlimit)
{
var countfield = document.getElementById(field2);
if ( field.value.length > maxlimit ) {
field.value = field.value.substring( 0, maxlimit );
return false;
} else {
countfield.value = maxlimit - field.value.length;
}
}
</script>
Hope this helps!
希望这可以帮助!
tip:
When merging the codes with your page, make sure the HTML elements(textarea
, input
) are loaded first before the scripts (Javascript functions)
将代码与页面合并时,请确保在脚本(Javascript函数)之前首先加载HTML元素(textarea,input)
更多相关文章
- arcgis api for js入门开发系列十 自定义Navigation控件样式风格
- 五十行javascript代码实现简单的双向数据绑定
- 在内容可编辑DIV中的选定文本周围包装bb代码
- 常用验证JS代码基础及实例
- 试着在我的javascript代码中理解“this”(一件事有效,另一件没有)
- 代码点火-如何从控制器返回Json响应
- 小弟初学网页(javascript),看不懂下面的代码。但又要完成任务 各位
- 使用jquery在textarea中的颜色代码
- 为什么我的javascript/jquery代码不能像预期的那样工作?