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

質問編集履歴

2

プログラムを追記

2017/08/24 18:50

投稿

aglkjggg
aglkjggg

スコア769

title CHANGED
File without changes
body CHANGED
@@ -6,6 +6,32 @@
6
6
 
7
7
  [https://jsfiddle.net/66vs2wu5/](https://jsfiddle.net/66vs2wu5/)
8
8
 
9
+ ```HTML
10
+ <input id="text1" type="text" placeholder="1">
11
+ <input id="text2" type="text" placeholder="2">
12
+ <input id="text3" type="text" placeholder="3">
13
+ ```
14
+ ```js
15
+ $('#text1').on('keyup', function (e) {
16
+ if (e.keyCode == 13) {
17
+ $('#text2').focus();
18
+ }
19
+ });
20
+
21
+ $('#text2').on('keyup', function (e) {
22
+ if (e.keyCode == 13) {
23
+ $('#text3').focus();
24
+ }
25
+ });
26
+
27
+ $('#text3').on('keyup', function (e) {
28
+ if (e.keyCode == 13) {
29
+ $('#text1').focus();
30
+ }
31
+ });
32
+ ```
33
+
34
+
9
35
  ### Ⅱ. 実現したいこと
10
36
  2点あります。
11
37
  ### 1. 冗長な部分がある
@@ -26,6 +52,60 @@
26
52
  どのようにすれば動的要素の追加に対応できますでしょうか。
27
53
  [https://jsfiddle.net/ukyrmLxh/](https://jsfiddle.net/ukyrmLxh/)
28
54
 
55
+ ```HTML
56
+ <button id="button1">追加する</button>
57
+
58
+ <div id="inputs">
59
+ <input id="text1" type="text" placeholder="1">
60
+ <input id="text2" type="text" placeholder="2">
61
+ <input id="text3" type="text" placeholder="3">
62
+ </div>
63
+ ```
64
+
65
+ ```js
66
+ let input_text_count = 3;
67
+
68
+ $('#button1').on('click', function (e) {
69
+ input_text_count++;
70
+
71
+ $("<input>", {
72
+ type: 'text',
73
+ id: 'text' + input_text_count,
74
+ placeholder: input_text_count
75
+ }).appendTo('#inputs');
76
+ });
77
+
78
+ $('#text1').on('keyup', function (e) {
79
+ if (e.keyCode == 13) {
80
+ $('#text2').focus();
81
+ }
82
+ });
83
+
84
+ $('#text2').on('keyup', function (e) {
85
+ if (e.keyCode == 13) {
86
+ $('#text3').focus();
87
+ }
88
+ });
89
+
90
+ $('#text3').on('keyup', function (e) {
91
+ if (e.keyCode == 13) {
92
+ $('#text4').focus();
93
+ }
94
+ });
95
+
96
+ $('#text4').on('keyup', function (e) {
97
+ if (e.keyCode == 13) {
98
+ $('#text5').focus();
99
+ }
100
+ });
101
+
102
+ $('#text5').on('keyup', function (e) {
103
+ if (e.keyCode == 13) {
104
+ $('#text1').focus();
105
+ }
106
+ });
107
+ ```
108
+
29
109
  ### 補足情報(言語/FW/ツール等のバージョンなど)
30
110
  - Windows 10 64bit
31
111
  - Chrome 60.0.3112.101(Official Build) (64 ビット)

1

Markdownの言語を修正

2017/08/24 18:50

投稿

aglkjggg
aglkjggg

スコア769

title CHANGED
File without changes
body CHANGED
@@ -12,7 +12,7 @@
12
12
  現状、以下のコードは`text1`, `text2`, `text3`全てほぼ同じ内容であるにも関わらず複数書いているため冗長と感じます。
13
13
  しかし、どのように書き直せば良いか分かりません。
14
14
  どのように書けば1つにまとめることができ、汎用性を高める事が出来ますでしょうか?
15
- ```
15
+ ```js
16
16
  $('#text1').on('keyup', function (e) {
17
17
  if (e.keyCode == 13) {
18
18
  $('#text2').focus();