質問編集履歴

2

説明文の変更

2018/01/16 01:49

投稿

kumakumatan
kumakumatan

スコア213

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,213 @@
1
+ データ構造($test_list)
2
+
3
+ ```ここに言語を入力
4
+
5
+ array(2) {
6
+
7
+ [0]=>
8
+
9
+ array(3) {
10
+
11
+ ["test_name"]=>
12
+
13
+ string(15) "テスト太郎"
14
+
15
+ ["test_d_name"]=>
16
+
17
+ string(6) "太郎"
18
+
19
+ ["test_address"]=>
20
+
21
+ string(6) "東京"
22
+
23
+ }
24
+
25
+ [1]=>
26
+
27
+ array(3) {
28
+
29
+ ["test_name"]=>
30
+
31
+ string(15) "テスト花子"
32
+
33
+ ["test_d_name"]=>
34
+
35
+ string(6) "花子"
36
+
37
+ ["test_address"]=>
38
+
39
+ string(6) "大阪"
40
+
41
+ }
42
+
43
+ }
44
+
45
+ ```
46
+
47
+ ■実現したい事
48
+
49
+ (1)「名前を追加」ボタンにて、ラベルを追加して、同じように「選択してください」ラベルから選んだ「$test_list[idx].test_name」から
50
+
51
+ 同じ配列インデックスの「test_d_name」と「test_address」の値をテキストボックスに表示させたいです。
52
+
53
+
54
+
55
+ idの重複はjavascriptではNGなので「test.tpl」の「id="select_test_info"」を「id="select_test_info1"」として、
56
+
57
+ 「名前を追加」ボタンを押して、ラベル追加した際2番目のidが「id="select_test_info2"」3番目のidが「id="select_test_info3"」
58
+
59
+ となっていくのでtplの下記部分をそれに対応したように、「document.getElementById('select_test_info2').addEventListener」
60
+
61
+ 「document.getElementById('select_test_info3').addEventListener」となるようにしたいのですが、どのように
62
+
63
+ したら良いかわからなく悩んでいます。
64
+
65
+
66
+
67
+ (2)ラベルを「選択してください」に戻すと、テキストボックス欄が「undefined」となってしまいます。
68
+
69
+ tplの下記部分に、
70
+
71
+ ```ここに言語を入力
72
+
73
+ if ( option.indexOf("選択してください") != -1) {
74
+
75
+ document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp;
76
+
77
+ }
78
+
79
+ ```
80
+
81
+ と記載して、「選択してください」ラベルがない場合のみ表示させようとしているのですが、うまく
82
+
83
+ いかなく悩んでいます。
84
+
85
+
86
+
87
+ 以上の2点をどのようにしたら良いかご教授いただきたくお願い致します。
88
+
89
+ ●test.tpl
90
+
91
+ ```html
92
+
93
+ <script language="JavaScript" src="test.js?t={$smarty.now}"></script>
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ <h3 class="Test_header">テスト</h3>
106
+
107
+ <table data-role="table" id="test_tab" data-mode="reflow" class="ui-responsive table-stroke">
108
+
109
+ <thead>
110
+
111
+ <tr>
112
+
113
+ <th width="5%">メイン</th>
114
+
115
+ <th width="30%">名前</th>
116
+
117
+ <th width="30%">名</th>
118
+
119
+ <th width="30%">住所</th>
120
+
121
+ <th width="5%">削除</th>
122
+
123
+ </tr>
124
+
125
+ </thead>
126
+
127
+ <tbody id="test_tbody">
128
+
129
+ {assign var='i' value=0}
130
+
131
+
132
+
133
+ <tr>
134
+
135
+ <td><input type="radio" id="in_test_radio{$i}" name="test_radio[{$i}]"/></td>
136
+
137
+ <td>
138
+
139
+ <select name="test_info" id="select_test_info1" data-native-menu="true">
140
+
141
+ <option value="">選択してください</option>
142
+
143
+ {section name=idx loop=$test_list}
144
+
145
+ <option value="{$test_list[idx].test_name|escape:"html"}"{if $test_list[idx].test_name == $test_list} selected{/if} data-disp="{$test_list[idx].test_d_name}" data-address="{$test_list[idx].test_address}">{$test_list[idx].test_name}</option>
146
+
147
+ {/section}
148
+
149
+ </select>
150
+
151
+ </td>
152
+
153
+ <td><input type="text" name="test_d_name[{$i}]" value="{$test_list[idx1].test_d_name|escape:"html"}" placeholder="名" /></td>
154
+
155
+ <td><input type="text" name="test_address[{$i}]" value="{$test_list[idx1].test_address|escape:"html"}" placeholder="住所" /></td>
156
+
157
+ <td><button type="button" id="btn_del_test{$i}" class="deleteTest ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete">削除</button>
158
+
159
+ </tr>
160
+
161
+ </tbody>
162
+
163
+ </table>
164
+
165
+
166
+
167
+ <div class="ui-field-contain">
168
+
169
+ <button type="button" id="btn_addName" class="addName ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-plus">名前を追加</button>
170
+
171
+ </div>
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ </div>
180
+
181
+ <script type="text/javascript">
182
+
183
+ var next_idx = $('#test_tbody').children().length;
184
+
185
+ // console.log(next_idx);
186
+
187
+ document.getElementById('select_test_info').addEventListener('change', function(event) {
188
+
189
+ var option = this.options[this.selectedIndex];
190
+
191
+ // console.log(option);
192
+
193
+ // if ( option.indexOf("選択してください") != -1) {
194
+
195
+ document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp;
196
+
197
+ // }
198
+
199
+ document.querySelector('input[name^="test_address"]').value = option.dataset.address;
200
+
201
+ }, false);
202
+
203
+ </script>
204
+
205
+ </body>
206
+
207
+ </html>
208
+
209
+ ```
210
+
1
211
  ●test.js(「名前を追加」ボタンを押した際)
2
212
 
3
213
  ```js
@@ -8,46 +218,22 @@
8
218
 
9
219
  {
10
220
 
221
+ var content = '';
222
+
223
+ var next_idx = $('#test_tbody').children().length;
224
+
225
+ next_idx++;
226
+
227
+ content += '<tr>';
228
+
229
+ content += '<td><input type="radio" id="in_test_radio{$i}" name="test_radio['+next_idx+']" value="'+next_idx+'" /></td>';
230
+
231
+ content += '<td><select name="test_info" id="select_test_info'+next_idx+'" data-native-menu="true">';
232
+
233
+ content += '</select></td>';
234
+
11
235
 
12
236
 
13
-
14
-
15
- console.log(test_name);
16
-
17
- // ↓
18
-
19
- // ここではtest_name[0]の値が表示されます。
20
-
21
-
22
-
23
- var content = '';
24
-
25
- var next_idx = $('#test_tbody').children().length;
26
-
27
- next_idx++;
28
-
29
-
30
-
31
- content += '{assign var=\'i\' value=0}';
32
-
33
- content += '<tr>';
34
-
35
- content += '<td><input type="radio" id="in_test_radio{$i}" name="test_radio['+next_idx+']" value="'+next_idx+'" /></td>';
36
-
37
- content += '<td><select name="test_info" id="select_test_info" data-native-menu="true">';
38
-
39
- content += '<option value="">選択してください</option>';
40
-
41
- content += '{section name=idx loop=$test_list}';
42
-
43
- content += '<option value="{$test_list[idx].test_name|escape:"html"}"{if $test_list[idx].test_name == $test_list} selected{/if} data-disp="{$test_list[idx].test_d_name}" data-address="{$test_list[idx].test_address}">{$test_list[idx].tes_name}</option>';
44
-
45
- content += '{/section}';
46
-
47
- content += '</select></td>';
48
-
49
-
50
-
51
237
  content += '<td><input type="text" name="test_d_name['+next_idx+']" value="" placeholder="名" /></td>';
52
238
 
53
239
  content += '<td><input type="text" name="test_address['+next_idx+']" value="" placeholder="住所" /></td>';
@@ -58,6 +244,10 @@
58
244
 
59
245
  $('#test_tbody').append(content);
60
246
 
247
+ // selectの中身を足す
248
+
249
+ $('#select_test_info'+next_idx).append($('#select_test_info').children().clone(true));
250
+
61
251
  //Table Tag 再構築
62
252
 
63
253
  $('#test_tbody').trigger('create');
@@ -70,174 +260,8 @@
70
260
 
71
261
  ```
72
262
 
73
- ●test.tpl
74
-
75
- ```html
76
-
77
- {if count($test_list) > 0}
78
-
79
- {assign var='i' value=0}
80
-
81
- {section name=idx1 loop=$test_list}
82
-
83
- test_name[{$i}] = '{$test_list[idx1].test_name}';
84
-
85
- test_d_name[{$i}] = '{$test_list[idx1].test_d_name}';
86
-
87
- {assign var='i' value=$i+1}
88
-
89
- {/section}
90
-
91
- {/if}
92
-
93
- {*
94
-
95
-
96
-
97
- ここでの値は正常に表示されています。
98
-
99
- test_name[0] = 'テスト太郎';
100
-
101
- test_d_name[0] = '太郎';
102
-
103
- test_name[1] = 'テスト花子';
104
-
105
- test_d_name[1] = '花子';
106
-
107
- *}
108
-
109
-
110
-
111
- <script language="JavaScript" src="test.js?t={$smarty.now}"></script>
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
- <h3 class="Test_header">テスト</h3>
124
-
125
- <table data-role="table" id="test_tab" data-mode="reflow" class="ui-responsive table-stroke">
126
-
127
- <thead>
128
-
129
- <tr>
130
-
131
- <th width="5%">メイン</th>
132
-
133
- <th width="30%">名前</th>
134
-
135
- <th width="30%">名</th>
136
-
137
- <th width="30%">住所</th>
138
-
139
- <th width="5%">削除</th>
140
-
141
- </tr>
142
-
143
- </thead>
144
-
145
- <tbody id="test_tbody">
146
-
147
- {assign var='i' value=0}
148
-
149
-
150
-
151
- <tr>
152
-
153
- <td><input type="radio" id="in_test_radio{$i}" name="test_radio[{$i}]"/></td>
154
-
155
- <td>
156
-
157
- <select name="test_info" id="select_test_info" data-native-menu="true">
158
-
159
- <option value="">選択してください</option>
160
-
161
- {section name=idx loop=$test_list}
162
-
163
- <option value="{$test_list[idx].test_name|escape:"html"}"{if $test_list[idx].test_name == $test_list} selected{/if} data-disp="{$test_list[idx].test_d_name}" data-address="{$test_list[idx].test_address}">{$test_list[idx].test_name}</option>
164
-
165
- {/section}
166
-
167
- </select>
168
-
169
- </td>
170
-
171
- <td><input type="text" name="test_d_name[{$i}]" value="{$test_list[idx1].test_d_name|escape:"html"}" placeholder="名" /></td>
172
-
173
- <td><input type="text" name="test_address[{$i}]" value="{$test_list[idx1].test_address|escape:"html"}" placeholder="住所" /></td>
174
-
175
- <td><button type="button" id="btn_del_test{$i}" class="deleteTest ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete">削除</button>
176
-
177
- </tr>
178
-
179
- </tbody>
180
-
181
- </table>
182
-
183
-
184
-
185
- <div class="ui-field-contain">
186
-
187
- <button type="button" id="btn_addName" class="addName ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-plus">名前を追加</button>
188
-
189
- </div>
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
- </div>
198
-
199
- <script type="text/javascript">
200
-
201
- document.getElementById('select_test_info').addEventListener('change', function(event) {
202
-
203
- var option = this.options[this.selectedIndex];
204
-
205
- console.log(option);
206
-
207
- if ( option.indexOf("選択してください") != -1) {
208
-
209
- document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp;
210
-
211
- }
212
-
213
- document.querySelector('input[name^="test_address"]').value = option.dataset.address;
214
-
215
- }, false);
216
-
217
- </script>
218
-
219
- </body>
220
-
221
- </html>
222
-
223
- ```
224
-
225
- 「選択してください」ラベルから選んだ「$test_list[idx].test_name」から同じ配列インデックスの「test_d_name」と「test_address」の値を
226
-
227
- テキストボックスに表示させています。
228
-
229
-
230
-
231
- その際、以下のご質問がございます。
232
-
233
-
234
-
235
- ★「名前を追加」ボタンにて「test.js」にて、ラベルを追加したいのですが、追加された「選択してください」ラベルを操作すると、
236
-
237
- 「$test_list[idx].test_name」が表示され、リスト選択できない状態です。
238
-
239
-
240
-
241
-
242
-
243
- データ構造($test_list)は
263
+ となっております。
264
+
265
+ ご教授いただきたくお願い申し上げます。
266
+
267
+ 宜しくお願いします。

1

2018/01/16 01:49

投稿

kumakumatan
kumakumatan

スコア213

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  {
10
10
 
11
- keepalive();
11
+
12
12
 
13
13
 
14
14