这是我的代码:
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;
}
}
如何显示特定文本框中剩余的字符数,限制为160?
答案 0 :(得分:47)
Dynamic HTML element function here中的代码,稍作修改和简化:
<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>
希望这有帮助!
提示:
将代码与页面合并时,请确保在脚本(Javascript函数)之前首先加载HTML元素(textarea
,input
)
答案 1 :(得分:28)
您可以将按键事件与输入框绑定,如果字符数超过160则返回false将解决问题jsfiddle
JS
$('textarea').keypress(function(){
if(this.value.length > 160){
return false;
}
$("#remainingC").html("Remaining characters : " +(160 - this.value.length));
});
HTML
<textarea></textarea> <span id='remainingC'></span>
答案 2 :(得分:6)
下面是一个简单的工作JS / HTML实现,它在删除输入时正确更新剩余的字符。
需要使用Bootstrap和JQuery来匹配布局和功能。 (根据包含的代码片段在JQuery 2.1.1上测试过)。
确保包含JS代码,以便在HTML之后加载它。如果您有任何问题,请给我留言。
$(document).ready(function() {
var len = 0;
var maxchar = 200;
$( '#my-input' ).keyup(function(){
len = this.value.length
if(len > maxchar){
return false;
}
else if (len > 0) {
$( "#remainingC" ).html( "Remaining characters: " +( maxchar - len ) );
}
else {
$( "#remainingC" ).html( "Remaining characters: " +( maxchar ) );
}
})
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="row">
<div class="col-sm-6 form-group">
<label>Textarea</label>
<textarea placeholder="Enter the textarea input here.. (limited to 200 characters)" rows="3" class="form-control" name="my-name" id="my-input" maxlength="200"></textarea><span id='remainingC'></span>
</div>
</div> <!--row-->
&#13;
答案 3 :(得分:4)
只需在keydown
个事件上注册一个Eventhandler,并检查该函数上输入字段的长度,并将其写入一个单独的元素。
请参阅 demo 。
var maxchar = 160;
var i = document.getElementById("textinput");
var c = document.getElementById("count");
c.innerHTML = maxchar;
i.addEventListener("keydown",count);
function count(e){
var len = i.value.length;
if (len >= maxchar){
e.preventDefault();
} else{
c.innerHTML = maxchar - len-1;
}
}
你也应该检查你服务器上的长度,因为Javascript可能被禁用或者用户想要故意做一些讨厌的事情。
答案 4 :(得分:2)
尝试
HTML
<textarea id="textarea" rows="8" cols="50" maxlength="100" ></textarea>
<div id="feedback"></div>
JS
$(document).ready(function() {
var max = 1000;
$('#feedback').html(max + 'characters remaining');
$('#textarea').keyup(function() {
var text_length = $('#textarea').val().length;
var text_remaining = max - text_length;
$('#feedback').html(text_remaining + ' characters remaining');
});
});
答案 5 :(得分:1)
这种方法如何将问题分成两部分:
textarea
以下显示递减计数器,当它达到零时变为红色,但仍允许用户键入。textarea
中的聊天数量大于160,我会使用单独的字符串长度验证程序(服务器端和客户端)来实际阻止提交表单。我的textarea
的ID为Message
,而我显示剩余字符数的span
的ID为counter
。当剩余字符数达到零时,将应用error
的css类。
var charactersAllowed = 160;
$(document).ready(function () {
$('#Message').keyup(function () {
var left = charactersAllowed - $(this).val().length;
if (left < 0) {
$('#counter').addClass('error');
left = 0;
}
else {
$('#counter').removeClass('error');
}
$('#counter').text('Characters left: ' + left);
});
});
答案 6 :(得分:1)
请尝试以下代码:
中的工作代码对于textArea,请使用:
<textarea id="txtBox"></textarea>
...
...
对于textBox,请使用:
<input type="text" id="txtBox"/>
<br>
<input type="text" id="counterBox"/>
<script>
var txtBoxRef = document.querySelector("#txtBox");
var counterRef = document.querySelector("#counterBox");
txtBoxRef.addEventListener("keydown",function(){
var remLength = 0;
remLength = 160 - parseInt(txtBoxRef.value.length);
if(remLength < 0)
{
txtBoxRef.value = txtBoxRef.value.substring(0, 160);
return false;
}
counterRef.value = remLength + " characters remaining...";
},true);
</script>
希望这有帮助!
答案 7 :(得分:0)
在这里试试这个代码......这是使用javascript onKeyUp()函数完成的......
<script>
function toCount(entrance,exit,text,characters) {
var entranceObj=document.getElementById(entrance);
var exitObj=document.getElementById(exit);
var length=characters - entranceObj.value.length;
if(length <= 0) {
length=0;
text='<span class="disable"> '+text+' <\/span>';
entranceObj.value=entranceObj.value.substr(0,characters);
}
exitObj.innerHTML = text.replace("{CHAR}",length);
}
</script>
答案 8 :(得分:0)
我需要这样的东西,我在jquery的帮助下给出的解决方案是:
<textarea class="textlimited" data-textcounterid="counter1" maxlength="30">text</textarea>
<span class='textcounter' id="counter1"></span>
使用此脚本:
// the selector below will catch the keyup events of elements decorated with class textlimited and have a maxlength
$('.textlimited[maxlength]').keyup(function(){
//get the fields limit
var maxLength = $(this).attr("maxlength");
// check if the limit is passed
if(this.value.length > maxLength){
return false;
}
// find the counter element by the id specified in the source input element
var counterElement = $(".textcounter#" + $(this).data("textcounterid"));
// update counter 's text
counterElement.html((maxLength - this.value.length) + " chars left");
});
现场演示Here
答案 9 :(得分:0)
HTML:
<form>
<textarea id='text' maxlength='10'></textarea>
<div id='msg'>10 characters left</div>
<div id='lastChar'></div>
</form>
JS:
function charCount() {
var textEntered = document.getElementById('text').value;
var msg = document.getElementById('msg');
var counter = (10-(textEntered.length));
msg.textContent = counter+' characters left';
}
var el = document.getElementById('text');
el.addEventListener('keyup',charCount,false);