老师你看看
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
<title>职位列表</title>
<style>
[type]{
width: 33%;
}
</style>
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
</head>
<body>
<button type="button"class="c1">员工列表</button>
<button type="button"class="c2">职位列表</button>
<button type="button"class="c3">部门列表</button>
<div class="container">
<h1 id="i1"></h1>
<h1 id="i2"></h1>
<h1 id="i3"></h1>
</div>
</body>
<script>
$(".c1").click(function () {
$.ajax({
url:"<%=basePath%>DemoServlet",
type:"post",
data:{
name:"c1"
},
dataType:"json",
success:function (result) {
var name1=result.name1;
var name2=result.name2;
var name3=result.name3;
// $(".container").remove();
$("#i1").text(name1);
$("#i2").text(name2);
$("#i3").text(name3);
}
})
})
$(".c2").click(function () {
$.ajax({
url:"<%=basePath%>DemoServlet",
type:"post",
data:{
name:"c2"
},
dataType:"json",
success:function (result) {
var name1=result.post1;
var name2=result.post2;
// $(".container").remove();
$("#i1").text(name1);
$("#i2").text(name2);
}
})
})
$(".c3").click(function () {
$.ajax({
url:"<%=basePath%>DemoServlet",
type:"post",
data:{
name:"c3"
},
dataType:"json",
success:function (result) {
var name1=result.section1;
var name2=result.section2;
var name3=result.section3;
// $(".container").remove();
$("#i1").text(name1);
$("#i2").text(name2);
$("#i3").text(name3);
}
})
})
</script>
</html>
package com.imooc.servlet;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/DemoServlet")
public class DemoServlet 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 {
String name = request.getParameter("name");
JSONObject jsonObject = null;
if ("c1".equals(name)) {
jsonObject = new JSONObject("{name1:'小明',name2:'小红',name3:'小谭'}");
} else if ("c2".equals(name)) {
jsonObject = new JSONObject("{post1:'职员',post2:'经理'}");
} else if ("c3".equals(name)) {
jsonObject=new JSONObject("{section1:'人事部',section2:'技术部',section:'无线事业部'}");
}
response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
}
}
怎么没达到想要的效果,如果加了$(".container").remove(); 页面直接不显示了
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星