テーブルの最終行や先頭には追加出来るんですが、テーブルの2行目の下に追加していきたいんですがでやり方がわかりません
<?php echo $this->Form->create('post', array('type' => 'file', 'url' => 'admin_add')); ?> <table> <tr> <th> タイトル</th> <td><?php echo $this->Form->input('title', array('label' => false, 'div' => false)); ?></td> </tr> <tr> <th> 画像</th> <td id="sample1_table"> <?php echo $this->Form->input('Post.0.photo', array('type' => 'file', 'label' => false)); ?> <?php echo $this->Form->input('Post.0.photo_dir', array('type' => 'hidden')); ?> </td> </tr> <tr> <td> <input type="button" value="行追加" onclick="insertRow('sample1_table')" /> </td> </tr>
//ここに追加していきたい
<script> /** * 行追加 */ function insertRow(id) { // テーブル取得 var table = document.getElementById(id); // 行を行末に追加 var row = table.insertRow(-1); // セルの挿入 var cell1 = row.insertCell(-1); var cell2 = row.insertCell(-1); // ボタン用 HTML var button = '<input type="button" value="行削除" onclick="deleteRow(this)" />'; // 行数取得 var row_len = table.rows.length; // セルの内容入力 cell1.innerHTML = '追加ボタン'; cell2.innerHTML = '<?php echo $this->Form->input('Post.0.photo_dir', array('type' => 'hidden')); ?><?php echo $this->Form->input('Post.0.photo', array('type' => 'file', 'label' => false)); ?>' + button; } /** * 行削除 */ function deleteRow(obj) { // 削除ボタンを押下された行を取得 tr = obj.parentNode.parentNode; // trのインデックスを取得して行を削除する tr.parentNode.deleteRow(tr.sectionRowIndex); } </script><tr> <th> 本文</th> <td><?php echo $this->Form->input('posttext', array('type' => 'text', 'label' => false, 'div' => false)); ?></td> </tr> </table> <?php echo $this->Form->end('Submit'); ?>
回答3件
あなたの回答
tips
プレビュー