HelloDalvik 2012-03-16 23:37
浏览 290
已采纳

jQuery和Java的复选框问题

请教一个问题:如何用jQuery获取左边那列“删除/批量”下选中的复选框,并在按“删除/批量”那个<a></a>标签后,把相关值提交到另一个页面去?求教!
此页面相关代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'process.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="jquery-1.4.1.js"></script>
    <script type="text/javascript">
    </script>
  </head>
  
  <body>
    <table width="745" border="1" cellspacing="0">
               <!--表头标题行-->
                <tr>
                    <td>会员信息管理</td>
                </tr>
                <tr>
                    <th width="84" scope="col"><a href="manager.jsp">删除/批量</a></th>
                    <th width="146" scope="col">会员编号</th>
                    <th width="92" scope="col">会员姓名</th>
                    <th width="52" scope="col">会员性别</th>
                    <th width="58" scope="col">联系电话</th>
                    <th width="220" scope="col">Email地址</th>
                </tr>
                <c:forEach var="p" items="${requestScope.lst}">
                    <tr>
                        <td><input type="checkbox" name="chose" /></td>
                        <td>${p.userID}</td>
                        <td>${p.userName}</td>
                        <td>${p.userSex}</td>
                        <td>${p.userPhone}</td>
                        <td>${p.email}</td>
                        
                    </tr>
                </c:forEach>
                
        </table>
  </body>
</html>
 
  • 写回答

9条回答 默认 最新

  • oxcow 2012-03-18 10:41
    关注

    a.jsp[code="html"]<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



    Insert title here $(function() { $("#allche").bind("click", function() { var ches = $(this).attr("checked"); if (!ches) { ches = false; } $.each($("#tCks :checkbox"), function(idx, val) { $(val).attr("checked", ches); }); }); $("#send").bind("click", function() { var aId = []; $.each($("#tCks :checked"), function(idx, val) { aId.push($(this).val()); }); if (aId.length == 0) { alert("请选择删除项"); return false; } var ids = aId.join(","); //使用1,或者使用2和3都可以 $(this).attr("href", "b.jsp?ids=" + ids); // 1 //window.location.href = "b.jsp?ids=" + ids; //2 //return false; // }); });



    批量删除










    <%
    for (int i = 1; i < 4; i++) {
    %>

    id name age
    http://www.w3.org/TR/html4/loose.dtd">



    Insert title here


    <%
    String ids = request.getParameter("ids");
    String[] aId = ids.split(",");
    //拿到删除用户的id数组了,将aId传递到你的数据操作部分,进行删除即可。
    %>
    你要删除的用户ID为<%=ids%>

    [/code]

    这里不管你是不是使用servlet还是jsp都是一样的。如果你使用的是servlet,那么把[code="js"]$(this).attr("href", "b.jsp?ids=" + ids);[/code]中的b.jsp换成你的servlet名称就行了,然后在该servlet中使用[code="java"]request.getParameter("ids");[/code]也就是b.jsp页面的代码进行获取就行了。最后将得到的id数组传到数据处理层进行处理就行了。

    当然这里建议不要使用href的形式,你可以提供个form表单,把选中的值放在一个隐藏域中机型表单提交,那样做也是可以的,要改动的就是在js中拿到选择的checked值,也就是[code="js"]var aId = [];
    $.each($("#tCks :checked"), function(idx, val) {
    aId.push($(this).val());
    });[/code]后,把aId.join(',')的值付给你的隐藏变量既可以,让后让form表单submit即可。这个你可以自己尝试下。

    我想我已经写的很明白了,如果还是弄不来那就再的无语了。呵呵,祝好运气了!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧