1. 程式人生 > >AJAX資料遍歷輸出

AJAX資料遍歷輸出

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge"
>
<title>半成品入庫操作</title> <script src="./js/jquery-2.1.1.min.js"></script> </head> <style> body{ background: #DDEEF2; } section h3,.form-div{ border:1px solid #BBDDE5; background: #F4FAFB; padding:5px; } ul li
{ list-style: none; margin-bottom:10px; } .form-div input[name='keyword']{ width:200px; } section table{ margin:0 auto; width:100%; border-top:1px solid #EEF8F9; border-left:1px solid #EEF8F9; } section table th{ background
: #BBDDE5
; border-right:1px solid #EEF8F9; border-bottom:1px solid #EEF8F9; line-height:30px; }
section table tr{ background: #fff; } section table td{ border-right:1px solid #EEF8F9; border-bottom:1px solid #EEF8F9; line-height:35px; } tr:hover{ background-color:#F4FAFB; }
</style> <body> <section> <h3>半成品入庫操作</h3> <div class="form-div"> <input type="text" name="keyword" size="15" id="keyword" placeholder="請精確搜尋半成品名稱"> <input type="submit" value=" 搜尋 " class="button" id="btn"> </div> <form action="bcpgoods.php" method="post" onsubmit="return check();"> <input type="hidden" name="act" value="insert_b"> <table cellspacing="0" cellpadding="0" border="0" id="tab"></table> </form> </section> <script> $('#btn').click(function(){ if($('#keyword').val() == ''){ alert('搜尋內容不能為空!'); }else{ $.ajax({ type:'post', url:'bcpgoods.php', data:{act:'serach',keyword:$('#keyword').val()}, success:function(data){ if(data == 'error'){ str = '<tr><td align="center">沒有搜尋到此類半成品!</td></tr>'; $('#tab').html(str); }else{ var res = JSON.parse(data); var str = '<tr><th>商品ID</th><th>半成品名稱</th><th>貨號</th><th>入庫數量</th></tr>'; $.each(res,function(index,obj){ str += '<tr>'; str += '<td>'+obj.goods_id+'</td>'; str += '<td>'+obj.goods_name+'</td>'; str += '<td>'+obj.goods_sn+'</td>'; str += '<td align="center"><input class="countt" name="count[]" style="width: 60px;" type="text" value=""></td>'; str += '</tr>'; str += '<input type="hidden" name="goods_id[]" value='+obj.goods_id+'>'; str += '<input type="hidden" name="goods_name[]" value='+obj.goods_name+'>'; }) str += '<tr><td align="center" colspan="4"><button>提交</button></td></tr>' $('#tab').html(str); } } }) } }) function check(){ var i = 0; $.each($('.countt'),function(k,v){ if($(this).val() == ''){ i++; } }) if(i == $('.countt').length){ alert('未錄入任何資料!'); return false; } } </script> </body> </html>