<html>
<table>
<tr>
<c:forEach items="${currentOrderedUser}" var="msg" >
<td>
<input type="checkbox" name="id" value="${msg.id}">
</td>
<td>
<input type="text" name="price" value="${msg.price}">
</td>
<td>
<input type="text" name="productName" value="${msg.productName}">
</td>
</tr>
</c:forEach>
<input type="submit" value="提交">
</table>
</html>
我用有是JSP语言
当用户单击"提交“按钮时,验证表单,如果用户未选中任何一个复选框(id),则提示用户至少选中一个,如果用户选中了一个复选框,则对选中该行的productName,infoProduct,price进行验证,使之不能为空,用Javascript程序验证
<table>
<tr>
<c:forEach items="${currentOrderedUser}" var="msg" >
<td>
<input type="checkbox" name="id" value="${msg.id}">
</td>
<td>
<input type="text" name="price" value="${msg.price}">
</td>
<td>
<input type="text" name="productName" value="${msg.productName}">
</td>
</tr>
</c:forEach>
<input type="submit" value="提交">
</table>
</html>
我用有是JSP语言
当用户单击"提交“按钮时,验证表单,如果用户未选中任何一个复选框(id),则提示用户至少选中一个,如果用户选中了一个复选框,则对选中该行的productName,infoProduct,price进行验证,使之不能为空,用Javascript程序验证
就是name=id+i price+i productName+i i为自增序列
这个应该可以实现确定的时候循环TR里面的对象,查找tagname为checkbox的判断他是否为选中.
选中的那个判断他的id是什么,通过截取最后一个循环队列的i值,获取对应的price+i的value值进行判断
function test(){
//以表格作索引
var len = tb.rows.length / 2;
var cbs = document.all.cb; if(len >= 1){
// 1行
if(len == 1){
if(cbs.checked){
//加入你的判断
}else{
alert("请选择");
}
}else{
// 多行
var sign = 0;
for(var i = 0; i < len; i ++){
//alert(cbs[i]);
if(cbs[i].checked){
sign = i;
break;
}
} if(sign != 0){
//加入你的判断
}else{
alert("请选择");
}
}
}else{
alert("没有记录");
}
}
</script>
<table border="1" id="tb">
<tr>
<td>checkbox</td>
<td><input type="checkbox" id="cb" name="cb" /></td>
</tr>
<tr>
<td align="right">name</td>
<td><input type="text" id="nm" name="nm" /></td>
</tr>
<!-- 隐藏以下,测试单个 -->
<tr>
<td>checkbox</td>
<td><input type="checkbox" id="cb" name="cb" /></td>
</tr>
<tr>
<td align="right">name</td>
<td><input type="text" id="nm" name="nm" /></td>
</tr>
<tr>
<td>checkbox</td>
<td><input type="checkbox" id="cb" name="cb" /></td>
</tr>
<tr>
<td align="right">name</td>
<td><input type="text" id="nm" name="nm" /></td>
</tr>
<tr>
<td>checkbox</td>
<td><input type="checkbox" id="cb" name="cb" /></td>
</tr>
<tr>
<td align="right">name</td>
<td><input type="text" id="nm" name="nm" /></td>
</tr>
</table>
<input type="button" value="test" onclick="test()" />
<body>
<script>
function mytest() {
var chk_sum = 0;
for (var i=0;i<document.getElementsByName('chk[]').length;i++) {
if (document.getElementsByName('chk[]')[i].checked == true) {
chk_sum += 1;
}
}
if (chk_sum == 0) {
alert('无选中');
return false;
}
for (var i=0;i<document.getElementsByName('chk[]').length;i++) {
if (document.getElementsByName('chk[]')[i].checked == true) {
if(chk_content(document.getElementsByName('content[]')[i]) == false)
return false;
}
}
}function chk_content(obj) {
if(obj.value == '') {
alert('不能为空');
obj.focus();
return false;
}
}
</script>
<form name='test' onsubmit="return mytest()">
<input type="checkbox" name="chk[]" value='11'><input type="text" name="content[]"><br>
<input type="checkbox" name="chk[]" value='22'><input type="text" name="content[]"><br>
<input type="checkbox" name="chk[]" value='34'><input type="text" name="content[]"><br>
<input type="checkbox" name="chk[]" value='52'><input type="text" name="content[]"><br>
<input type="submit" value="test">
<form>
</body>
</html>
找到解决方法了
<%@ page import="com.ruanko.model.OrderedUser;"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%
List<OrderedUser> users = new ArrayList<OrderedUser>();
OrderedUser u1 = new OrderedUser();
u1.setId(1L);u1.setPrice(222.20);u1.setProductName("Java编程思想");
OrderedUser u2 = new OrderedUser();
u2.setId(1L);u2.setPrice(232.20);u2.setProductName("Java核心技术");
users.add(u1); users.add(u2);
request.setAttribute("currentOrderedUser", users);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.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">
<script type="text/javascript">
/**
这是一个工具函数
*/
function $(id) {
return document.getElementById(id);
}
function validate() {
var objs = document.getElementsByTagName('input');
var num = 0;
var indexs = [];
for(var i=0 ;i<objs.length; i++) {
if(objs[i].type.toLowerCase() == 'checkbox') {
if(objs[i].checked == true) {
num += 1;
indexs.push(i+1);
}
}
}
if(num <= 0) {
alert('至少选中一个!'); return false;
}else {
for(var j = 0; j<indexs.length; j++) {
var priceValue = $('price_' + indexs[j]).value;
var productNameValue = $('productName_' + indexs[j]).value;
if(priceValue == null || '' == priceValue) {
alert('产品' + indexs[j] + '的价格不能为空');
return false;
}
if(productNameValue == null || productNameValue == '') {
alert('产品' + indexs[j] + '的名称不能为空');
return false;
}
}
}
}
</script>
</head>
<body>
<form id="myForm" onsubmit="return validate();">
<table>
<c:forEach items="${currentOrderedUser}" var="msg" varStatus="status">
<tr>
<td>
<input type="checkbox" name="id" value="${msg.id}">
</td>
<td>
<input type="text" name="price" value="" id="price_${status.index+1}">
</td>
<td>
<input type="text" name="productName" value="" id="productName_${status.index+1}">
</td>
</tr>
</c:forEach>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>写得很粗糙,但是功能实现了,搞了我半天