jQuery 获取checkbox选中的值form表单提交例子

 前台页面

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
<link rel="stylesheet" type="text/css" href="../Admin/role/css/index.css">
</head>
<script type="text/javascript" src="../Admin/role/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
var zhi 
function addClick(){ 
$(document).ready(function(){
var idsstr = " ";
var zhi = " ";
$(".mian_b_bg_xz input[name=checkbox]").each(function(){ //遍历table里的全部checkbox
idsstr += $(this).val() + ","; //获取所有checkbox的值
if($(this).attr("checked")) //如果被选中
zhi += $(this).val() + ","; //获取被选中的值
});
if(idsstr.length > 0) //如果获取到
idsstr = idsstr.substring(0, idsstr.length - 1); //把最后一个逗号去掉
if(zhi.length > 0) //如果获取到
zhi = zhi.substring(0, zhi.length - 1); //把最后一个逗号去掉
alert("所有checkbox的值:" + idsstr);
alert("被选中checkbox的值:" + zhi);
document.form.action="addRole.do?zhi="+zhi
document.form.submit()
});
}

</script>
<body class="mian_bj">
<div class="mian_top_01">
<div class="mian_top_r"></div>
<div class="mian_top_l"></div>
<div class="mian_top_c">
</div>
<div class="mian_b">
<form action="" method="post" name="form">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="mian_b_bg_xz">
<tr>
<td width="5%" class="mian_b_bg_lm">&nbsp;</td>
<td colspan="4" class="mian_b_bg_lm">添加角色</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="7%" valign="top">角色名称:</td>
<td width="20%">
<input type="text" class="input_01" name="rolename" id="name"> </td>
<td width="7%"></td>
<td width="56%"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td valign="middle">权限管理:</td>
<td colspan="3">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="mian_b_bg_xz">
<tr bgcolor="#BDB76B">
<td valign="top" colspan="3"><O>客户管理权限</O></td>
</tr>
<tr>
<td width="3%">&nbsp;</td>
<td colspan="2">
<input name="checkbox" type="checkbox" value="1">客户基础信息 &nbsp;&nbsp;&nbsp;
<input name="checkbox" type="checkbox" value="2">客户接待记录&nbsp;&nbsp;&nbsp;
<input name="checkbox" type="checkbox" value="3">销售机会
</td>
</tr>
<tr bgcolor="#BDB76B">
<td valign="top" colspan="3"><O>交易管理权限</O></td>
</tr>
<tr>
<td width="3%">&nbsp;</td>
<td colspan="2">
<input name="checkbox" type="checkbox" value="4">开盘管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="5">订单管理&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="8">合同管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="6">付款方式管理&nbsp;&nbsp;&nbsp;<input id="Checkbox77" type="checkbox" value="7">报告审核
</td>
</tr>
<tr>
<td valign="top" colspan="3" bgcolor="#BDB76B">
<O>后台管理权限</O></td>
</tr>
<tr>
<td width="3%">&nbsp;</td>
<td colspan="2">
<input name="checkbox" type="checkbox" value="9">用户管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="11">角色管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="11">组织机构管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="12">项目管理</br>
<input name="checkbox" type="checkbox" value="13">楼栋管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="14">单元管理&nbsp;&nbsp;&nbsp;<input name="checkbox" type="checkbox" value="15">房间管理
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="4">
<div align="center">
<input type="button" value="确定" οnclick="addClick()" > 
</div>
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>

后台接值  form表单中的值和拼接的字符串全都取到

 

@RequestMapping("/addRole.do")
public String addRole(Role role,String zhi){
System.out.println("role"+role);
String[] split = zhi.split(",");
for (int i = 0; i < split.length; i++) {
String string = split[i];
System.out.println("zhi"+string);
}  
return "redirect:SelectAll.do";
}

 

在 HTML 中,checkbox 是一种 input 类型,用于让用户选择一项或多项选项。当用户提交包含 checkbox 的表单时,需要获取用户所选的。 可以通过以下方式来获取 checkbox: 1. 使用 JavaScript: ```html <form> <input type="checkbox" name="fruit" value="apple"> Apple<br> <input type="checkbox" name="fruit" value="banana"> Banana<br> <input type="checkbox" name="fruit" value="orange"> Orange<br> <button onclick="getCheckboxValue()">Submit</button> </form> <script> function getCheckboxValue() { var checkboxes = document.getElementsByName("fruit"); var values = []; for (var i=0; i<checkboxes.length; i++) { if (checkboxes[i].checked) { values.push(checkboxes[i].value); } } console.log(values); } </script> ``` 在此示例中,我们使用了 `document.getElementsByName()` 方法来获取所有名称为 "fruit" 的 checkbox 元素,然后使用 `for` 循环遍历所有 checkbox,如果 checkbox选中,则将它的添加到数组中。最后,我们将数组打印到控制台。 2. 使用 jQuery: ```html <form> <input type="checkbox" name="fruit" value="apple"> Apple<br> <input type="checkbox" name="fruit" value="banana"> Banana<br> <input type="checkbox" name="fruit" value="orange"> Orange<br> <button id="submit-btn">Submit</button> </form> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $("#submit-btn").click(function() { var values = []; $("input[name='fruit']:checked").each(function() { values.push($(this).val()); }); console.log(values); }); </script> ``` 在此示例中,我们使用 jQuery 的选择器来获取所有名称为 "fruit" 的 checkbox 元素,并使用 `each()` 方法遍历所有选中checkbox。然后,我们将它们的添加到数组中,并将数组打印到控制台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值