简体   繁体   English

计算键盘上的剩余字符

[英]Counting remaining characters on keyup

I want to write a program that calculates the length of the remaining characters while I type in the textbox but the code is not working. 我想编写一个在文本框中键入内容时计算剩余字符长度的程序,但是代码无法正常工作。

Below is the HTML code: 以下是HTML代码:

<!DOCTYPE html>
<html lang="en">
    <head>
         <title>
         </title>
         <meta charset="utf-8" />
         <link rel="stylesheet" type="text/css" href="css/custom.css" />
    </head>
    <body>
        <textarea rows="10" cols="30" maxlength="100">
        </textarea>
        <p id="area_description"></p>
    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
    <script type="text/javascript" src="js/custom.js" ></script>
    </body>
</html> 

Below is the code in custom.js : 以下是custom.js的代码:

 $(document).ready(function(){
    $('#area_description').html('100 characters remaining.');
    $('textarea').on('keyup',function(){
        var x = $('textarea').val().length();
        var y = 100-x;
        $('#area_description').html(y+'characters remaining');
    });
 });

You have a typo. 你有错字 Replace .length() with .length . .length()替换为.length

 $(document).ready(function(){ $('#area_description').html('100 characters remaining.'); $('textarea').on('keyup',function(){ var x = $('textarea').val().length; var y = 100-x; $('#area_description').html(y+'characters remaining'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea rows="10" cols="30" maxlength="100"> </textarea> <p id="area_description"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM