jsp實現表格的增刪功能
<%@ 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 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>
<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">
<h1><button type="submit" onclick="addHero()">添加</button>
<button type="" onclick="changeHero()">修改</button></h1>
</form>
<%
%>
</body>
</html>
jsp實現表格的增刪功能