有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在jsp中删除动态表元素?我应该在控制器中写什么?

这是我的JSP页面。这里的表是动态生成的。每个条目都有一个删除按钮。当我点击任何删除按钮时,我应该如何编码删除

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<title>View & Delete Restaurant</title>
<jsp:include page="headerAdmin.jsp" />
</head>
<body>
<c:choose>
<c:when test="${not empty restaurantList}">
<h5>Following restaurants are listed in the application:</h5>
    <table border="2">
        <tr><th>Id</th><th>Name</th><th>Address</th><th>Zip</th><th>Cuisine</th></tr>
        <c:forEach var="o" items="${restaurantList}">
            <tr>
                <td width="5%">${o.restaurantId}</td>
                <td width="20%">${o.restaurantName}</td>
                <td width="20%">${o.address}</td>
                <td width="20%">${o.pincode}</td>   
                <td width="20%">${o.cuisine}</td>
                <td width="20%"><input type="button" value="Delete From List" name="DeleteFromList" /></td> 
            </tr>
        </c:forEach>
    </table>
</c:when>
<c:otherwise>There are no restaurants to be deleted from list</c:otherwise>
</c:choose>
</body>
</html>

截图:

My JSP Page


共 (1) 个答案

  1. # 1 楼答案

    要从DOM中删除行,可以使用JavaScript,添加以下内容

    <script>
    function deleteRow(r) {
        var i = r.parentNode.parentNode.rowIndex;
        document.getElementById("myTable").deleteRow(i);
    }
    </script>
    

    您需要为<table>元素提供一个ID,以获取JS中的表。(例如"myTable"-或使用getElementByTagName)。 可以按如下方式添加单元格

    <td width="20%"><input type="button" value="Delete From List" name="DeleteFromList"  onclick="deleteRow(this)"></td>