[問題] json 回傳多個值

看板Ajax作者 (路人)時間12年前 (2013/05/31 18:23), 編輯推噓1(108)
留言9則, 2人參與, 最新討論串1/1
我想從資料庫撈資料(多筆)放回頁面 無奈小弟不會用json 得到的都是object Object 懇請各位前輩賜教 或是提供關鍵字 ~"~ 以下是用jQuery autocomplete改的 http://jqueryui.com/autocomplete/#remote ----------search.php---------- $stmt = $conn->prepare('select id,name from table'); $stmt->execute(); while($row = $stmt->fetch()) { $return_arr[] = $row; } echo json_encode($return_arr); ------------------------------ 會得到多行 example: [{"id":455,"0":455,"name":"\u6c5f","1":"\u6c5f"}, {"id":393,"0":393,"name":"\u8521","1":"\u8521"}] ----------jQuery---------- $( "#birds" ).autocomplete({ source: "search.php", minLength: 2, response: function( event, ui ) { log( ui ); } }); function log( message ) { $( "<div>" ).text( messageitem.value ).prependTo( "#output" ); $( "#output" ).scrollTop( 0 ); } ----------html---------- <div> <label for="birds">Birds: </label> <input id="birds" /> </div> <div> <div id="output"> Here </div> </div> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.81.228.254 ※ 編輯: cold514 來自: 219.81.228.254 (05/31 18:26)

05/31 20:22, , 1F
autocomplete 預設回傳的是一維陣列,你這樣就變二維了
05/31 20:22, 1F

05/31 20:52, , 2F
自問自答 找到答案了 觀念太差 http://ppt.cc/ZZ5G
05/31 20:52, 2F

05/31 20:53, , 3F
while($row = $stmt->fetch()) {
05/31 20:53, 3F

05/31 20:54, , 4F
$return_arr[] = array(
05/31 20:54, 4F

05/31 20:54, , 5F
'c_id'=>$row['c_id'],
05/31 20:54, 5F

05/31 20:54, , 6F
'name'=>$row['name']); }
05/31 20:54, 6F

05/31 20:56, , 7F
response: function( event, ui ) {
05/31 20:56, 7F

05/31 20:56, , 8F
log( ui.content[1].name );
05/31 20:56, 8F

05/31 20:56, , 9F
}
05/31 20:56, 9F
文章代碼(AID): #1Hg7eHa- (Ajax)
文章代碼(AID): #1Hg7eHa- (Ajax)