1. 程式人生 > >JSON.parse(data); 使用 及 json陣列的輸出

JSON.parse(data); 使用 及 json陣列的輸出

資料遍歷 JSON.parse(data);

後臺返回的資料格式:

[{"ygbz":"\u5f20\u71d5","user_name":"123321"},{"ygbz":"\u738b\u971e123","user_name":"jhd6244"}]

<body>
    <header id="header">
        <div class="header_l header_return"> <a class="ico_10" href="" > 返回 </a> </div>
        <h1
>
授權物件 </h1> </header> <div class="search_box"> <div class="box_bac" style=""> <!-- 用於點選檢索 --> <div id="btn" title="查詢" style="width:31px;height: 30px;background-color:none;float: left;"></div> <input
type="text" name="" id="tell" style="background:none; margin-left:-5px;" placeholder="輸入員工名字搜尋">
</div> </div> <!-- 檢索框結束 --> <!-- 主題部分開始 --> <div id="desc_box"> {foreach from=$data item=fo} <div></div> <a
href="phone_system.php?act=adminAuth&id={$fo.user_id}&num={$num}" >
<div class="desc" id="desc" > <ul class="desc_ul" id="desc_ul" > <div style="display: inline-block; width: 80%; overflow: hidden;"> <li style="" id="name">{$fo.ygbz}</li> <li style="color:#999;" id="comments">賬號:{$fo.user_name}</li> </div> <div style="width:6%;line-height: 60px;float: right;margin-top:-3px;color:#999;"> > </div> </ul> </div> </a> {/foreach} </div> <!-- 主題部分結束 --> </body> <script style="text/javascript"> $(function () { /********************檢索框開始*******************************/ $('#btn').click(function(){ // alert('111'); $.ajax({ type:'post', url:'erp_authAllot.php', async: false, // tell 是檢索框的內容 data:{act:'index',info:$('#tell').val()}, success:function(data){ var data = JSON.parse(data); console.log(data); $('#desc_box div:gt(0)').remove(); var s = ''; $.each(data,function(index,obj){ s += '<div style="width: 100%;min-height:60px; background-color: #fff;border-bottom:1px solid #c8c8c8;">' + '<ul style="margin-left:5%; font-size: 16px; padding: 5px;">' + '<li>' + obj.ygbz + '</li>' + '<li style="color:#999;">' + obj.user_name + '</li>' + '</ul>' + '</div>'; }) $('#desc_box div').append(s); } }) }); // 鍵盤事件 onkeypress=function(event){ if(event.keyCode == 13){ $('#btn').click(); } }; // 失焦事件 $("#tell").blur(function (){ $('#btn').click(); }); }); </script> </html>