I encountered a case at work that I have never written about before. I spent half an afternoon trying to write it out. It was so frustrating and a sense of accomplishment! Of course, this is nothing to a js expert, but it is a small step forward for my own js ability.
Case introduction: We often see that some websites have textarea text boxes. When you enter, there is a text prompt below how many words you can enter. Today we are going to implement this function. Of course, since a page has several textareas, it is not possible to use a single js logic for control, and it must be encapsulated in a small way. Of course, there are still some flaws in my package, but the basic functions are achieved.
First introduce the single textarea implementation case
html part:
<textarea id="text_txt1"></textarea> <span id ="num_txt1">剩余可输入600字</span>
js part:
$(function(){ $('#text_txt1').on('keyup',function(){ var txtval = $('#text_txt1').val().length; console.log(txtval); var str = parseInt(600-txtval); console.log(str); if(str > 0 ){ $('#num_txt1').html('剩余可输入'+str+'字'); }else{ $('#num_txt1').html('剩余可输入0字'); $('#text_txt1').val($('#text_txt1').val().substring(0,600)); //这里意思是当里面的文字小于等于0的时候,那么字数不能再增加,只能是600个字 } //console.log($('#num_txt').html(str)); }); })
Then introduce multiple textarea implementation cases on the same page
function changeLength(obj,num){ obj.on('keyup',function(){ var txtval = obj.val().length; //console.log(txtval); var str = parseInt(600-txtval); //console.log(str); if(str > 0 ){ num.html('剩余可输入'+str+'字'); }else { num.html('剩余可输入0字'); obj.val(obj.val().substring(0, 600)); } //console.log($('#num_txt').html(str)); }); } $(function(){ //我这里有四个,所以调用4次 changeLength($('#text_txt1'),$('#num_txt1')); changeLength($('#text_txt2'),$('#num_txt2')); changeLength($('#text_txt3'),$('#num_txt3')); changeLength($('#text_txt4'),$('#num_txt4')); });
Of course, the actual number of words required here can also be encapsulated inside the function, but I will not encapsulate it. In this way, when text is entered, the remaining word count will be automatically displayed inside the span. When the input value reaches the maximum value, the remaining word count will be displayed as 0, and new content cannot be filled in. When text is deleted, span can dynamically obtain the remaining number of words.
The following is other people's code, this time I also borrowed some other people's writing methods
html:
<p class="family_v2"> <p class="nickname_v2">简介:</p> <textarea id="content" name="sign" style="height:60px;overflow-y: hidden;" onkeyup="changeLength(this,60)" class="nicknameBox_v2 brief_box_v2"> </textarea> <p class="limit_num_v2"> <h3>60</h3> </p> </p>
js:
//验证textarea的长度 function changeLength(obj,lg){ var len = $(obj).val(); $(obj).next().find("h3").text(lg-len.length); if(len.length>=lg){ $(obj).next().find("h3").text(0); $(obj).val(len.substring(0,lg)); } }
The above article on how to obtain the dynamic remaining word count in a textarea is all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.
For more related articles on how to obtain the dynamic remaining word count in textarea, please pay attention to the PHP Chinese website!