teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

inputのほうも記述

2016/12/27 13:37

投稿

s8_chu
s8_chu

スコア14731

answer CHANGED
@@ -21,4 +21,29 @@
21
21
  </script>
22
22
  </body>
23
23
  </html>
24
+ ```
25
+ 追記
26
+ ---
27
+ mukkunさんにコメントしていただいたinputを使う方法を追記します。
28
+ ```HTML
29
+ <!DOCTYPE html>
30
+ <html>
31
+ <head>
32
+ <meta charset="UTF-8">
33
+ <title>タイトル</title>
34
+ </head>
35
+ <body>
36
+ <form>
37
+ <input type="number" max="999" id="text1">
38
+ </form>
39
+ <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
40
+ <script>
41
+ $('#text1').on('input', function () {
42
+ if ($(this).val().length >= 4) {
43
+ $(this).val($(this).val().slice(0, 3));
44
+ }
45
+ });
46
+ </script>
47
+ </body>
48
+ </html>
24
49
  ```

1

修正

2016/12/27 13:37

投稿

s8_chu
s8_chu

スコア14731

answer CHANGED
@@ -14,7 +14,7 @@
14
14
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
15
15
  <script>
16
16
  $('#text1').on('keyup', function () {
17
- if ($(this).val().length > 3) {
17
+ if ($(this).val().length >= 4) {
18
18
  $(this).val($(this).val().slice(0, 3));
19
19
  }
20
20
  });