jQueryでテキストボックスの文字数をカウントしてみます。
bindメソッドを使ってkeyupとchange時に文字数をカウント(正確にはlength)したものを表示しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8" /> <title>サンプル</title> <script src="http://code.jquery.com/jquery.js"></script> <!-- コーディング部分 --> <script type="text/javascript"> $(function(){ $("#text").bind("keyup change", function() { $("#count").text($(this).val().length); }); }); </script> <!-- コーディング部分 --> </head> <body> <input type="text" id="text" /> <p id="count"></p> </body> </html> |
デモです。