<%@ page import="java.util.List" %> <%@ page import="bean.Hero" %> <%@ page import="java.util.ArrayList" %>
<% response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); String xh=request.getParameter("xh"); String xm=request.getParameter("xm"); String ch=request.getParameter("ch"); List<Hero> list=new ArrayList<Hero>();
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>hero</title> </head> <body> <%
Hero h1=new Hero();
h1.setCh("及时雨");
h1.setXm("宋江");
h1.setXh("1");
Hero h2=new Hero();
h2.setCh("黑旋风");
h2.setXm("李逵");
h2.setXh("2");
list.add(h1);
list.add(h2);
%> <script>
//增加功能
function addHero() {
<%
Hero h=new Hero();
h.setXh(xh);
h.setXm(xm);
h.setCh(ch);
boolean flag=true;
for(Hero he:list){
if(he.getXh().equals(xh)){
flag=false;
%>alert("该用户已存在,请勿重复添加")<%
break;
}
}
if (flag==true){
list.add(h);
}
%>
}
//删除功能
function deletHero(elm) {
elm.parentElement.parentElement.remove();
}
</script>
<table style="border: beige;background-color: bisque" border="1px" id="ta" style="text-align: center;">
<tr>
<td>序号</td>
<td>姓名</td>
<td>绰号</td>
<td colspan="2" style="text-align: center;">操作</td>
</tr>
<% for(Hero hero:list){ //如果为空,就不添加 if(hero.getCh()==null){ break; } %><tr> <td><%=hero.getXh()%></td> <td><%=hero.getXm()%></td> <td><%=hero.getCh()%></td> <td><span onclick="changeHero(this)" style="cursor: pointer;">修改</span></td> <td><span onclick="deletHero(this)" style="cursor: pointer;">删除</span></td> </tr> <% }%>
</table>
<form action="" method="post" name="form1"> 序号:<input type="text" name="xh" id="xh"><br> 姓名:<input type="text" name="xm" id="xm"><br> 绰号:<input type="text" name="ch" id="ch"> <button type="submit" onclick="addHero()">添加</button> <button type="" onclick="changeHero()">修改</button> </form> <%
%>
</body> </html>