Home > Web Front-end > JS Tutorial > JavaScript records the number of characters in the text box and detects changes in the number of characters_javascript skills

JavaScript records the number of characters in the text box and detects changes in the number of characters_javascript skills

WBOY
Release: 2016-05-16 16:34:01
Original
990 people have browsed it

I recently encountered such a problem while working on a project. It is necessary to count the text entered by the user in the text box and display it below. Because what we are doing is an SMS sending platform, and now our country’s SMS service, If your message exceeds 70 characters, two text messages will be sent to you. So we are required to show the user how many words he has entered. It is convenient for users to know that my information will be divided into several pieces.

I downloaded a code and uploaded it. When I started using it, it was ok and not bad, but after using it for a while, I discovered a problem. It means that after you delete the text, there is a problem with the change in word count. Later, I did some research and found the problem. onKeyDown="showLen(this)" onKeyUp="showLen(this). In the past, only one of the methods was used each time, and no one used it. These two methods count when you press the keyboard and release the keyboard. Okay, I’ll upload the program

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
<mce:script language="javascript"><!-- 
function showLen(obj) 
{ 
document.getElementById("contentLen").value=obj.value.length; 
document.getElementById("smsnum").value=Math.floor(obj.value.length/70)+1; 
} 
// --></mce:script> 
</HEAD> 
<BODY> 

<textarea name="content" cols="60" rows="5" id="content" onKeyDown="showLen(this)" onKeyUp="showLen(this)">
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template