帮助从 HTML 表格中删除字段

发布于 2024-11-04 10:54:28 字数 1657 浏览 0 评论 0原文

所以我不确定如何标记这个,但基本上我有一个动态加载的 HTML 表,其中包含可以删除的值,目前我正在使用复选框让用户决定要删除哪一行。到目前为止,这是我的代码:

      <table id="comments" align="center" width="59%">
    <thead>
      <th class="headerClass" width="7%">Delete</th>
      <th width="15%" class="headerClass">Date</th>
      <th class="headerClass" width="15%">Employee</th>
      <th class="headerClass">Comment</th>
    </thead>
      <tbody>           
<%
  while (result.next()) { 

        commentDate = StringUtils.defaultString(result.getString("commentDate"));
        commentUser = StringUtils.defaultString(result.getString("cName"));
        comment = StringUtils.defaultString(result.getString("xbs_comment"));

%>

      <tr>
        <td class="normal"><input type="checkbox" class="checkbox" /></td>
        <td class="normal"><%=commentDate%></td>
        <td class="normal"><%=commentUser%></td>
        <td class="normal" width="68%"><%=comment%></td>                
      </tr> 
  </tbody>     
</table>    
  <label for="comment"><h4>Add a Comment:</h4></label> 
  <textarea cols="105" id="comment" name="comment" rows="4"></textarea> 
<br />

<div align="center" class="submit">
  <input class="submit" width="10%" type="submit" name="submit" id="submit" value="Submit" />
</div>

基本上我有一个带有评论的表格,用户可以使用提交按钮添加评论,但我希望他们也能够使用复选框来删除评论。我是否需要另一个带有删除按钮的表单标签,或者我可以只使用这个提交按钮吗?另外,如果我确实保留了复选框,我如何让程序的后端(函数和 servlet)知道选中了哪个?感谢您的帮助!

So I am not sure exactly how to tag this one, but basically I have a dynamically loaded HTML table, that contains values that will can be deleted, currently I am using check boxes for the user to decide which row to delete. Here is my code so far:

      <table id="comments" align="center" width="59%">
    <thead>
      <th class="headerClass" width="7%">Delete</th>
      <th width="15%" class="headerClass">Date</th>
      <th class="headerClass" width="15%">Employee</th>
      <th class="headerClass">Comment</th>
    </thead>
      <tbody>           
<%
  while (result.next()) { 

        commentDate = StringUtils.defaultString(result.getString("commentDate"));
        commentUser = StringUtils.defaultString(result.getString("cName"));
        comment = StringUtils.defaultString(result.getString("xbs_comment"));

%>

      <tr>
        <td class="normal"><input type="checkbox" class="checkbox" /></td>
        <td class="normal"><%=commentDate%></td>
        <td class="normal"><%=commentUser%></td>
        <td class="normal" width="68%"><%=comment%></td>                
      </tr> 
  </tbody>     
</table>    
  <label for="comment"><h4>Add a Comment:</h4></label> 
  <textarea cols="105" id="comment" name="comment" rows="4"></textarea> 
<br />

<div align="center" class="submit">
  <input class="submit" width="10%" type="submit" name="submit" id="submit" value="Submit" />
</div>

Basically I have a table with comments, the user can add a comment using the submit button, but I would like them to also be able to use the check box to delete comments. Do i need another form tag w/ a delete button, or can I use just this one submit button? Also if I do stay with the check boxes, how do I let the back end of my program(function and servlet) know which was checked? Thanks for any help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2024-11-11 10:54:28

您可以与相同的提交一起执行此操作。您也可以通过ajax删除评论,删除成功后可以重新加载页面。

以下是使用同一提交按钮删除评论的一些方法。我假设用户有权删除表中的所有评论。

but I would like them to also be able to use the check box to delete comments

为此,您必须在复选框上分配一个对于每条评论都是唯一的值。通常是表示每条评论的行中的主键或 ID。

对每个复选框使用单个名称来执行多次删除。下面的示例

comments.jsp

<form name="commentForm" action="addDelete.jsp">
    <div>
<table>
    <thead>
        <tr>
            <th>No.</th><th>Date</th><th>User</th><th>Comments</th>
        </tr>
    </thead>
    <tbody>
        <% 
          for(Comment cmnt : commentList){
        %>
            <tr>
                <td><input type="checkbox" value="<%=cmnt.getCmntId()%>" name="cmntId" /></td>
                <td><%=cmnt.getCmntDate()%></td>
                <td><%=cmnt.getCmntUser()%></td>
                <td><%=cmnt.getCmntComment()%></td>
            </tr>
        <%              
          }
        %>
    </tbody>
</table>
<textarea cols="50" rows="10" name="newComment">
</textarea>
<br />
<input type="submit" value="Delete" />
<input type="hidden" name="userId" value="Id_of_the_user">
</div>
</form>

这只是一个示例,因此请更多地关注有复选框的部分。

addDelete.jsp

现在,在 adddelete.jsp 上,您可以有两个具有不同功能的查询。第一个是添加新评论,第二个是删除评论。

要获取要删除的评论列表,请将其存储在数组中。并且还获取其他字段。

String[] cmntIds = request.getParameterValue("cmntId"); //This store all the cmntId that are checked in the previous checkbox.
String newComment = request.getParameterValue("newComment");
String userId = request.getParameterValue("userId"); //This can be in a session

//Some function you may need
deleteComment(cmntIds); //Execute a query to delete comment using the cmntIds
inserNewComment(newComment, userId); //Execute a query to add the new comment using newComment and userId if there is a comment attached.

除了这一部分之外,我希望您能够处理完成您想做的事情所需的功能。

You can do this along with the same submit. And also you can delete comment via ajax then you may reload the page upon success in deletion.

Here is some of the way to delete the comment by using the same submit button. And I assume that the user has the access to delete all the comments in the table.

but I would like them to also be able to use the check box to delete comments

To do this you must assign a value on the checkbox that are unique for every comments. Usually the primary key or ID in the row that represent each comment.

Use a single name for every checkbox to do a multiple delete. Sample below

comments.jsp

<form name="commentForm" action="addDelete.jsp">
    <div>
<table>
    <thead>
        <tr>
            <th>No.</th><th>Date</th><th>User</th><th>Comments</th>
        </tr>
    </thead>
    <tbody>
        <% 
          for(Comment cmnt : commentList){
        %>
            <tr>
                <td><input type="checkbox" value="<%=cmnt.getCmntId()%>" name="cmntId" /></td>
                <td><%=cmnt.getCmntDate()%></td>
                <td><%=cmnt.getCmntUser()%></td>
                <td><%=cmnt.getCmntComment()%></td>
            </tr>
        <%              
          }
        %>
    </tbody>
</table>
<textarea cols="50" rows="10" name="newComment">
</textarea>
<br />
<input type="submit" value="Delete" />
<input type="hidden" name="userId" value="Id_of_the_user">
</div>
</form>

This is just an example, so focus more on the part where have a checkbox.

addDelete.jsp

Now on the adddelete.jsp, you could have two queries with different function. first is for adding new comments and second is the deletion of the comment(s).

To get the list of comment to be delete store it in an array. And also get the other fields.

String[] cmntIds = request.getParameterValue("cmntId"); //This store all the cmntId that are checked in the previous checkbox.
String newComment = request.getParameterValue("newComment");
String userId = request.getParameterValue("userId"); //This can be in a session

//Some function you may need
deleteComment(cmntIds); //Execute a query to delete comment using the cmntIds
inserNewComment(newComment, userId); //Execute a query to add the new comment using newComment and userId if there is a comment attached.

Beyond this part I hope you can handle what function you will need to do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文