您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用Ajax怎么動(dòng)態(tài)顯示并操作表信息,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
js文件內(nèi)容如下:
$(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頁(yè)面代碼如下:
<%@include file="../inc/top.jsp"%> <script src="Fenlei.js"></script> <div class="Classify"> <h4 align="center">項(xiàng)目管理信息表</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內(nèi)容如下:
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"); //項(xiàng)目分類名稱 String flag = request.getParameter("flag"); String id = request.getParameter("id"); //項(xiàng)目分類Id FenleiService cs = new FenleiService(); JSONObject json = new JSONObject(); if("all".equals(flag)) { List<FenleiBean> list = cs.getAll(); //獲取所有的項(xiàng)目分類信息 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是一種在無(wú)需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分網(wǎng)頁(yè)的技術(shù),可以通過(guò)在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新。
關(guān)于使用Ajax怎么動(dòng)態(tài)顯示并操作表信息就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。