您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用Ajax怎么動態顯示并操作表信息,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
js文件內容如下:
$(function () { $.ajaxSetup({ async:false }); var url = "/Task/Fenlei"; //servlet的url data = {}; data.flag = "all"; $.post(url,data,function (result) { for(var i=0;i<result.getAll.length;i++){ var id = result.getAll[i].fenlei_Id; var name = result.getAll[i].fenlei_Name; var newrow = "<tr id='tr"+id+"'><td>"+result.getAll[i].fenlei_Id+"</td><td id='td"+id+"'>"+result.getAll[i].fenlei_Name+ "</td><td><button onclick='del("+id+")''>刪除</button><button onclick='edit("+id+")'>修改</button></td></tr>" $("#AllInfo tr:last").after(newrow); } },"json"); $("#add").click(function () { addData={}; var name = $("#name").val(); addData.name = name; addData.flag = "add"; $.post(url,addData,function (result) { var id = result.aFenlei.fenlei_Id; var name = result.aFenlei.fenlei_Name; var newrow = "<tr id='tr"+id+"'><td>"+result.aFenlei.fenlei_Id+"</td><td id='td"+id+"'>"+result.aFenlei.fenlei_Name+ "</td><td><button onclick='del("+id+")'>刪除</button><button onclick='edit("+id+")'>修改</button></td></tr>" $("#AllInfo tr:last").after(newrow); },"json"); }); }); function del(id) { console.log(id); var url = "/Task/Fenlei"; delData = {}; delData.flag = "delete"; delData.id = id; $.post(url,delData,function (result) { if(result) { alert("刪除成功"); $("#tr"+id).remove(); } else { alert("刪除失敗"); } },"json"); }; function edit(id) { var url = "/Task/Fenlei"; editData = {}; editData.flag = "update"; var oldname = $("#td"+id).text(); $("#td"+id).html("<input type='text'class='Input' id='new' name='FenleiName' value='"+oldname+"'/>"); $("#new").blur(function () { var newname = $(".Input").val(); editData.newname = newname; console.log(newname); $("#td"+id).html("<td id='td"+id+"'>"+newname+"</td>"); $.post(url,editData,function(result){ if(result) { alert("修改成功"); } else { alert("修改失敗"); } },"json"); }); }
jsp頁面代碼如下:
<%@include file="../inc/top.jsp"%> <script src="Fenlei.js"></script> <div class="Classify"> <h4 align="center">項目管理信息表</h4> <div class="divBack"> <img src="#" /> </div> <div class="divAdd"> 分類名稱:<input type="text" id="name"/> <button type="button" class="btn-primary" id="add">添加</button> </div> <table class="table"id="AllInfo"> <tr> <th>分類Id</th> <th>分類名稱</th> <th>操作</th> </tr> </table> </div> <%@include file="../inc/bottom.jsp"%>
處理的servlet內容如下:
public class FenleiServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); //項目分類名稱 String flag = request.getParameter("flag"); String id = request.getParameter("id"); //項目分類Id FenleiService cs = new FenleiService(); JSONObject json = new JSONObject(); if("all".equals(flag)) { List<FenleiBean> list = cs.getAll(); //獲取所有的項目分類信息 json.put("getAll",list); response.getWriter().print(json.toJSONString()); } if("add".equals(flag)) { //增加操作。 FenleiBean cb = cs.add(name); json.put("aFenlei",cb); response.getWriter().print(json.toJSONString()); } if("delete".equals(flag)) { //刪除操作 boolean result = cs.delete(id); System.out.println(flag); System.out.println(result); if(result){ json.put("result",result); json.put("msg","刪除成功"); response.getWriter().print(json.toJSONString()); System.out.println(json.toJSONString()); } else { json.put("result",result); json.put("msg","刪除失敗"); response.getWriter().print(json.toJSONString()); } } if("update".equals(flag)) { //更新信息 System.out.println(flag); String newname = request.getParameter("newname"); System.out.println("---------------update newname"+newname); boolean result = cs.update(newname); if(result){ json.put("result",result); json.put("msg","修改成功"); response.getWriter().print(json.toJSONString()); System.out.println(json.toJSONString()); } else { json.put("result",result); json.put("msg","修改失敗"); response.getWriter().print(json.toJSONString()); } } } }
</pre><pre name="code" class="javascript">
ajax是一種在無需重新加載整個網頁的情況下,能夠更新部分網頁的技術,可以通過在后臺與服務器進行少量數據交換,使網頁實現異步更新。
關于使用Ajax怎么動態顯示并操作表信息就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。