温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jsp实现表格的增删功能

发布时间:2020-04-03 19:49:02 来源:网络 阅读:1164 作者:l123j 栏目:web开发

<%@ 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 h2=new Hero();
h2.setCh("及时雨");
h2.setXm("宋江");
h2.setXh("1");
Hero h3=new Hero();
h3.setCh("黑旋风");
h3.setXm("李逵");
h3.setXh("2");
list.add(h2);
list.add(h3);

%>
<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  border="1px" id="ta" >
<tr>
<td>序号</td>
<td>姓名</td>
<td>绰号</td>
<td colspan="2" >操作</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">
<h2><button type="submit" onclick="addHero()">添加</button>
<button type="" onclick="changeHero()">修改</button></h2>
</form>
<%

%>

</body>
</html>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI