jsp頁面中實現複選框全選和反選、選中刪除功能__js

來源:互聯網
上載者:User

jsp中表單頁面

<span style="font-size:12px;"><form action="<%= request.getContextPath() %>/DelectServlet" method=post>  <tr>    <th>    全選/反選<input type="checkbox" id="checkall" name="checkall" onclick="checkAll(checkall)" /><br>         <input type="submit" value="刪除"  onclick="return confirm('確定刪除選擇項。')" align="left">     <input type="button"  value="修改" onclick="doDelete()" align="left">    </th>  <th>編號</th>  <th>使用者名稱</th>  <th>密碼</th>  <th>姓名</th>  <th>郵箱</th>  <th>性別</th>  <th>生日</th>  <th>愛好</th>  <th>薪水</th>  <th>其他</th>  <th>角色</th>  </tr>   <c:forEach var="person" items="${jsp}">    <tr>    <td align ="center"><input type="checkbox" id=' ${person.id}' name='info' value= '${person.id}' /></td>    <td>${person.id}</td>    <td>${person.username}</td>    <td>${person.passwd}</td>    <td>${person.realname}</td>    <td>${person.email}</td>    <td>${person.sex}</td>    <td>${person.birth}</td>    <td>${person.habit}</td>    <td>${person.salary}</td>    <td>${person.other}</td>    <td>${person.role}</td>     </tr>        </c:forEach>     </form></span>
js實現全選反選功能

<script type="text/javascript">function checkAll(checkall) {  arr = document.getElementsByName('info' ); if (checkall.checked == true) { for(i=0;i<arr.length;i++){ arr[i].checked = true; }}else{for(i=0;i<arr.length;i++){ if((arr[i]).checked==false){arr[i].checked = true;}else{arr[i].checked = false; }}}}  </script>

實現選中刪除的servlet類

package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import Dao.Dao;import Dao.DaoJDBCImpl;/** * Servlet implementation class DelectServlet */public class DelectServlet extends HttpServlet {private static final long serialVersionUID = 1L;          protected void processRequest(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {   response.setContentType("text/html;charset=UTF-8");   PrintWriter out=response.getWriter();   String[] selectdelete = request.getParameterValues("info");    String ids ="";    for(int i=0;i<selectdelete.length;i++){    ids += "'"+selectdelete[i]+"'";    if(i!=selectdelete.length-1) ids+=",";    try {Dao dao = new DaoJDBCImpl();dao.remove(ids);out.print("<script language='javascript'>alert('刪除成功。');window.location.href='ShowServlet';</script>");out.flush();out.close();} catch (Exception e) {// TODO Auto-generated catch block}   }   }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */   protected void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {       processRequest(request, response);   } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */   protected void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {       processRequest(request, response);   }}

jdbc資料庫刪除資料

    //刪除人員    public void remove(String id) throws Exception{     dbc = new DaoJDBCImpl();     String sql="delete from person where id in ("+id+")";     PreparedStatement pst = dbc.getConnection().prepareStatement(sql);     pst.executeUpdate();        pst.close();        dbc.close();    }


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.