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

質問編集履歴

2

説明文の変更

2018/01/16 02:54

投稿

kumakumatan
kumakumatan

スコア213

title CHANGED
File without changes
body CHANGED
@@ -1,34 +1,48 @@
1
- ●test.js(「名前を追加」ボンを押した際)
1
+ デー構造($test_list)
2
- ```javascript
2
+ ```ここに言語を入力
3
+ array(2) {
4
+ [0]=>
5
+ array(3) {
3
- //名前の行を追加する
6
+ ["test_name"]=>
7
+ string(15) "テスト太郎"
4
- $(document).on("click", ".addName", function()
8
+ ["test_d_name"]=>
9
+ string(6) "太郎"
10
+ ["test_address"]=>
11
+ string(6) "東京"
5
- {
12
+ }
13
+ [1]=>
6
- keepalive();
14
+ array(3) {
15
+ ["test_name"]=>
16
+ string(15) "テスト花子"
17
+ ["test_d_name"]=>
18
+ string(6) "花子"
19
+ ["test_address"]=>
20
+ string(6) "大阪"
21
+ }
22
+ }
23
+ ```
24
+ ■実現したい事
25
+ (1)「名前を追加」ボタンにて、ラベルを追加して、同じように「選択してください」ラベルから選んだ「$test_list[idx].test_name」から
26
+ 同じ配列インデックスの「test_d_name」と「test_address」の値をテキストボックスに表示させたいです。
7
27
 
8
- var content = '';
9
- var next_idx = $('#test_tbody').children().length;
10
- next_idx++;
11
- content += '<tr>';
12
- content += '<td><input type="radio" id="in_test_radio{$i}" name="test_radio['+next_idx+']" value="'+next_idx+'" /></td>';
13
- content += '<td><select name="test_info" id="select_test_info" data-native-menu="true">';
14
- content += '<option value="">選択してください</option>';
15
- content += '{section name=idx loop=$test_list}';
16
- 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>';
17
- content += '{/section}';
18
- content += '</select></td>';
19
-
20
- content += '<td><input type="text" name="test_d_name['+next_idx+']" value="" placeholder="名" /></td>';
21
- content += '<td><input type="text" name="test_address['+next_idx+']" value="" placeholder="住所" /></td>';
22
- content += '<td><button type="button" id="btn_del_test'+next_idx+'" class="deleteTest ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete">削除</button></td>';
23
- content += '</tr>';
24
- $('#test_tbody').append(content);
25
- //Table Tag 再構築
26
- $('#test_tbody').trigger('create');
28
+ idの重複はjavascriptではNGなので「test.tpl」の「id="select_test_info"」を「id="select_test_info1"」として、
29
+ 「名前を追加」ボタンを押して、ラベル追加した際2番目のidが「id="select_test_info2"」3番目のidが「id="select_test_info3"」
30
+ となっていくのでtplの下記部分をそれに対応したように、「document.getElementById('select_test_info2').addEventListener」
31
+ 「document.getElementById('select_test_info3').addEventListener」となるようにしたいのですが、どのように
32
+ したら良いかわからなく悩んでいます。
27
33
 
34
+ (2)ラベルを「選択してください」に戻すと、テキストボックス欄が「undefined」となってしまいます。
28
- return false;
35
+ tplの下記部分に、
36
+ ```html
37
+ if ( option.indexOf("選択してください") != -1) {
38
+ document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp;
29
- });
39
+ }
30
40
  ```
41
+ と記載して、「選択してください」ラベルがない場合のみ表示させようとしているのですが、うまく
42
+ いかなく悩んでいます。
31
43
 
44
+ 以上の2点をどのようにしたら良いかご教授いただきたくお願い致します。
45
+
32
46
  ●test.tpl
33
47
  ```html
34
48
  <script language="JavaScript" src="test.js?t={$smarty.now}"></script>
@@ -54,7 +68,7 @@
54
68
  <tr>
55
69
  <td><input type="radio" id="in_test_radio{$i}" name="test_radio[{$i}]"/></td>
56
70
  <td>
57
- <select name="test_info" id="select_test_info" data-native-menu="true">
71
+ <select name="test_info" id="select_test_info1" data-native-menu="true">
58
72
  <option value="">選択してください</option>
59
73
  {section name=idx loop=$test_list}
60
74
  <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>
@@ -76,56 +90,46 @@
76
90
 
77
91
  </div>
78
92
  <script type="text/javascript">
93
+ var next_idx = $('#test_tbody').children().length;
94
+ // console.log(next_idx);
79
95
  document.getElementById('select_test_info').addEventListener('change', function(event) {
80
96
  var option = this.options[this.selectedIndex];
81
- console.log(option);
97
+ // console.log(option);
82
- if ( option.indexOf("選択してください") != -1) {
98
+ // if ( option.indexOf("選択してください") != -1) {
83
99
  document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp;
84
- }
100
+ // }
85
101
  document.querySelector('input[name^="test_address"]').value = option.dataset.address;
86
102
  }, false);
87
103
  </script>
88
104
  </body>
89
105
  </html>
90
106
  ```
107
+ ●test.js(「名前を追加」ボタンを押した際)
108
+ ```js
109
+ //名前の行を追加する
110
+ $(document).on("click", ".addName", function()
111
+ {
112
+ var content = '';
113
+ var next_idx = $('#test_tbody').children().length;
114
+ next_idx++;
115
+ content += '<tr>';
116
+ content += '<td><input type="radio" id="in_test_radio{$i}" name="test_radio['+next_idx+']" value="'+next_idx+'" /></td>';
117
+ content += '<td><select name="test_info" id="select_test_info'+next_idx+'" data-native-menu="true">';
118
+ content += '</select></td>';
119
+
120
+ content += '<td><input type="text" name="test_d_name['+next_idx+']" value="" placeholder="名" /></td>';
121
+ content += '<td><input type="text" name="test_address['+next_idx+']" value="" placeholder="住所" /></td>';
122
+ content += '<td><button type="button" id="btn_del_test'+next_idx+'" class="deleteTest ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete">削除</button></td>';
123
+ content += '</tr>';
124
+ $('#test_tbody').append(content);
125
+ // selectの中身を足す
91
- 「選択してください」ラベルから選んだ「$test_list[idx].test_name」から同じ配列インデックスの「test_d_name」と「test_address」の値を
126
+ $('#select_test_info'+next_idx).append($('#select_test_info').children().clone(true));
92
- テキストボックスに表示させています。
127
+ //Table Tag 再構築
128
+ $('#test_tbody').trigger('create');
93
129
 
94
- その際、以下の3点ご質問がございます。
95
-
96
- (1)「test_name」をラベル選択した後に「選択してください」ラベルを選択すると、「test_d_name」と「test_address」の値
97
- が「undefined」となってしまいます。
98
- 「if ( option.indexOf("選択してください") != -1)」にてラベルに「選択してください」がない場合、「test_d_name」の値をセットする
99
- ようにしているのですが、セットされません。どのようにしたらいいのでしょうか。
100
-
101
- (2)「名前を追加」ボタンにて「test.js」にて、ラベルを追加したいのですが、追加された「選択してください」ラベルを操作すると、
102
- 「$test_list[idx].test_name」が表示され、リスト選択できない状態です。
103
-
104
- (3)ラジオボタンを一つだけ選択できるようにしたいのですが、どのようにしたら実装できますでしょうか。
105
-
106
- データ構造($test_list)は
130
+ return false;
131
+ });
107
132
  ```
108
- array(2) {
109
- [0]=>
110
- array(3) {
111
- ["test_name"]=>
112
- string(15) "テスト太郎"
113
- ["test_d_name"]=>
114
- string(6) "太郎"
115
- ["test_address"]=>
116
- string(6) "東京"
117
- }
118
- [1]=>
119
- array(3) {
120
- ["test_name"]=>
121
- string(15) "テスト花子"
122
- ["test_d_name"]=>
123
- string(6) "花子"
124
- ["test_address"]=>
125
- string(6) "大阪"
126
- }
127
- }
128
- ```
129
133
  となっております。
130
134
  ご教授いただきたくお願い申し上げます。
131
135
  宜しくお願いします。

1

記載の変更

2018/01/16 02:53

投稿

kumakumatan
kumakumatan

スコア213

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- ●test.js(名前を追加ボタンを押した際)
1
+ ●test.js(名前を追加ボタンを押した際)
2
2
  ```javascript
3
3
  //名前の行を追加する
4
4
  $(document).on("click", ".addName", function()