jQuery中ajax請求后臺返回json數(shù)據(jù)并渲染HTML的方法
更新時間:2018年08月08日 11:30:17 作者:卷筒紙
今天小編就為大家分享一篇jQuery中ajax請求后臺返回json數(shù)據(jù)并渲染HTML的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
html實例
<table border="0" class="restaurant_food" cellspacing="0" cellpadding="1"> <input type="text" name="dishes" value="" class="seek_product" placeholder="請輸入菜名"/> <button type='button' class="btn_nor" onclick="seek_product()">搜索</button> <tr> <th width="30%">序號</th> <th width="70%">菜名</th> </tr> <tr data-id=""> <td></td> <td class="tl"> <p></p></td> </tr> </table>
jquery實例
function seek_product(){ var product = $('.seek_product').val(); $.ajax({ type:'get', url:'/Cash/Index/seek_product', data:{name:product}, success:function(res){ var data = eval('('+res+')'); var len = data.length; var cm = ""; if(len > 0){ for(var i = 0; i < len; i++){ cm += '<tr data-id='+data[i]['id']+'>'; cm += '<td>'; cm += i+1; cm += '</td>'; cm += '<td class="tl">'; cm += '<p>'+data[i]["name"]+'</p>'; cm += '</td>'; cm += '</tr>'; console.log(cm); $('.restaurant_food').html(cm); } }else{ $('.restaurant_food').html('抱歉,沒有這道菜!'); } } }) }
php實例
//搜索菜 public function seek_product(){ $shop_id = session("cashShopId"); $name = I('get.name'); $map['name'] = array('like','%'.$name.'%'); $map['shop_id'] = $shop_id; $map['status'] = 1; $productList = M('product')->field('id,name')->where($map)->select(); echo json_encode($productList); }
以上這篇jQuery中ajax請求后臺返回json數(shù)據(jù)并渲染HTML的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于jQuery實現(xiàn)表格內(nèi)容的篩選功能
這篇文章主要介紹了基于jQuery實現(xiàn)表格內(nèi)容的篩選功能的相關(guān)資料,需要的朋友可以參考下2016-08-08兩個select之間option的互相添加操作(jquery實現(xiàn))
兩個select,將其中一個select選中的選項添加到另一個select中,或者點擊全部添加按鈕將所有的option都添加過去.2009-11-11jQuery動態(tài)操作表單示例【基于table表格】
這篇文章主要介紹了jQuery動態(tài)操作表單,結(jié)合實例形式分析了jQuery針對table表格的數(shù)據(jù)添加、刪除、元素修改、提交等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12easyui中combotree循環(huán)獲取父節(jié)點至根節(jié)點并輸出路徑實現(xiàn)方法
下面小編就為大家?guī)硪黄猠asyui中combotree循環(huán)獲取父節(jié)點至根節(jié)點并輸出路徑實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11