码迷,mamicode.com
首页 > Web开发 > 详细

jsp实现表格的增删功能

时间:2018-07-25 17:06:56      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:func   break   submit   lag   character   import   rem   put   name   

<%@ 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">
<h1><button type="submit" onclick="addHero()">添加</button>
<button type="" onclick="changeHero()">修改</button></h1>
</form>
<%

%>

</body>
</html>

jsp实现表格的增删功能

标签:func   break   submit   lag   character   import   rem   put   name   

原文地址:http://blog.51cto.com/13670525/2150086

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!