データ構造($test_list)
array(2) { [0]=> array(3) { ["test_name"]=> string(15) "テスト太郎" ["test_d_name"]=> string(6) "太郎" ["test_address"]=> string(6) "東京" } [1]=> array(3) { ["test_name"]=> string(15) "テスト花子" ["test_d_name"]=> string(6) "花子" ["test_address"]=> string(6) "大阪" } }
■実現したい事
(1)「名前を追加」ボタンにて、ラベルを追加して、同じように「選択してください」ラベルから選んだ「$test_list[idx].test_name」から
同じ配列インデックスの「test_d_name」と「test_address」の値をテキストボックスに表示させたいです。
idの重複はjavascriptではNGなので「test.tpl」の「id="select_test_info"」を「id="select_test_info1"」として、
「名前を追加」ボタンを押して、ラベル追加した際2番目のidが「id="select_test_info2"」3番目のidが「id="select_test_info3"」
となっていくのでtplの下記部分をそれに対応したように、「document.getElementById('select_test_info2').addEventListener」
「document.getElementById('select_test_info3').addEventListener」となるようにしたいのですが、どのように
したら良いかわからなく悩んでいます。
(2)ラベルを「選択してください」に戻すと、テキストボックス欄が「undefined」となってしまいます。
tplの下記部分に、
html
1if ( option.indexOf("選択してください") != -1) { 2 document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp; 3 }
と記載して、「選択してください」ラベルがない場合のみ表示させようとしているのですが、うまく
いかなく悩んでいます。
以上の2点をどのようにしたら良いかご教授いただきたくお願い致します。
●test.tpl
html
1<script language="JavaScript" src="test.js?t={$smarty.now}"></script> 2 3・ 4・ 5・ 6 7<h3 class="Test_header">テスト</h3> 8<table data-role="table" id="test_tab" data-mode="reflow" class="ui-responsive table-stroke"> 9<thead> 10 <tr> 11 <th width="5%">メイン</th> 12 <th width="30%">名前</th> 13 <th width="30%">名</th> 14 <th width="30%">住所</th> 15 <th width="5%">削除</th> 16 </tr> 17</thead> 18<tbody id="test_tbody"> 19{assign var='i' value=0} 20 21<tr> 22 <td><input type="radio" id="in_test_radio{$i}" name="test_radio[{$i}]"/></td> 23 <td> 24 <select name="test_info" id="select_test_info1" data-native-menu="true"> 25 <option value="">選択してください</option> 26 {section name=idx loop=$test_list} 27 <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> 28 {/section} 29 </select> 30 </td> 31 <td><input type="text" name="test_d_name[{$i}]" value="{$test_list[idx1].test_d_name|escape:"html"}" placeholder="名" /></td> 32 <td><input type="text" name="test_address[{$i}]" value="{$test_list[idx1].test_address|escape:"html"}" placeholder="住所" /></td> 33 <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> 34</tr> 35</tbody> 36</table> 37 38<div class="ui-field-contain"> 39 <button type="button" id="btn_addName" class="addName ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-plus">名前を追加</button> 40</div> 41・ 42・ 43・ 44</div> 45 <script type="text/javascript"> 46 var next_idx = $('#test_tbody').children().length; 47// console.log(next_idx); 48 document.getElementById('select_test_info').addEventListener('change', function(event) { 49 var option = this.options[this.selectedIndex]; 50// console.log(option); 51// if ( option.indexOf("選択してください") != -1) { 52 document.querySelector('input[name^="test_d_name"]').value = option.dataset.disp; 53// } 54 document.querySelector('input[name^="test_address"]').value = option.dataset.address; 55 }, false); 56 </script> 57</body> 58</html>
●test.js(「名前を追加」ボタンを押した際)
js
1//名前の行を追加する 2$(document).on("click", ".addName", function() 3{ 4 var content = ''; 5 var next_idx = $('#test_tbody').children().length; 6 next_idx++; 7 content += '<tr>'; 8 content += '<td><input type="radio" id="in_test_radio{$i}" name="test_radio['+next_idx+']" value="'+next_idx+'" /></td>'; 9 content += '<td><select name="test_info" id="select_test_info'+next_idx+'" data-native-menu="true">'; 10 content += '</select></td>'; 11 12 content += '<td><input type="text" name="test_d_name['+next_idx+']" value="" placeholder="名" /></td>'; 13 content += '<td><input type="text" name="test_address['+next_idx+']" value="" placeholder="住所" /></td>'; 14 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>'; 15 content += '</tr>'; 16 $('#test_tbody').append(content); 17 // selectの中身を足す 18 $('#select_test_info'+next_idx).append($('#select_test_info').children().clone(true)); 19 //Table Tag 再構築 20 $('#test_tbody').trigger('create'); 21 22 return false; 23});
となっております。
ご教授いただきたくお願い申し上げます。
宜しくお願いします。
あなたの回答
tips
プレビュー