Jquery でボタンを押すアクションで行を追加する処理に各行にプルダウンを設定しようとしています。
ところがAjaxで取得したデータが表示されず困っています。
//モデル public function Get_Data(){ //POSTデータ $post_array = array(); $post_array = $this->input->post(); $sql = " select code as code,name as name"; $sql .= " from ref "; $query = $this->db->query($sql); $conv = array(); if($query->num_rows() > 0){ foreach($query->result() as $row){ $ary = array(); $ary[$row->code]= $row->name; $conv[] = $ary; } } return $conv; }
//コントローラ public function index(){ $ary =$this->amu054->Get_Data(); echo json_encode($ary); }
//JS jQuery(function(){ jQuery('#add_table').click(function(){ var len = $("#tbl tbody").children().length; if(len > 6){ $("#add_table").attr("disabled","disabed"); } $.ajax({ type: "POST", url: "/admin/au054", dataType: "json", cache: false, success: function(data, status){ var element; var row; for(var i in data){ element += ' <option value ="' + data[i].code + '">' + data[i].name + '</option>'; } var count = $("#tbl tbody").children().length; row += '<tr>'; row += '<td><select name="code[]" id="sns">' + element + '</td>'; row += '<td><input type="text" name="prm" size="20" /></td>'; row += '</tr>'; $("#tbl").append(row); }, error: function(XMLHttpRequest, status, errorThrown){ alert("エラーが発生しました!"); } }); }); jQuery('#del_bottom').click(function(){ if (jQuery('tbody tr').length) { jQuery('tbody tr:last').remove(); } }); });
//HTML <table class="table" id="tbl"> <tbody> <tr> <th><select name="code[]" id="code"> <option value ="0"></option> <option value ="01">aaaa</option> <option value ="02">bbbb</option> <option value ="03">cccc</option> <option value ="04">dddd</option> <option value ="05">eeee</option> </select> </th> <td><input type="text" name="prm[]" size="20" /></td> </tr> </tbody> </table> <p><input type="button" id="add_table" value="追加する" /> <input type="button" id="del_table_sns" value="削除する" /></p>
ボタンを押して行を追加
そしてプルダウンとテキストボックスの行を生成という操作を行いたいです。
うまくデータが表示されず困っています。
どうぞご教授のほど宜しくお願いいたします。
回答1件
あなたの回答
tips
プレビュー