分别通过Jquery和Document遍历name值相同的input内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>添加用户</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="<%=basePath%>assets/css/header/header.css">
<link rel="stylesheet" type="text/css" href="assets/css/table/table.css">
<script type="text/javascript" src="assets/js/jquery/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function SubmitAdd(){
//alert(12345)
$.ajax({
type : "POST",
url : "ajax/AddUser",
dataType : 'text',
data : $("#users").serialize(),
success:function(result){
//alert(result);
var arr = result.split(";");
},
error:function(){
}
})
}
function Check(){
var arr =[];
$("input[name='userAccount']").each(function(){
arr.push($(this).val());
console.log("id:"+$(this).attr('id'));
})
console.log(arr);
console.log("----华丽的分割线,jquery-----");
var account = document.getElementsByName("userAccount");//$("input[name='userAccount']");
for(var i=0;i<account.length;i++){
//设置背景颜色
//account[i].style.backgroundColor = 'red';
console.log(i+"--"+account[i].value);
}
}
</script>
</head>
<body>
<div id="all">
<div id="header"><iframe src="<%=basePath%>pages/menu/menu.jsp"></iframe></div>
<div id="bodyer">
<div style="position:relative;width:90%;height:60%;padding:4%;">
<form id="users">
<table id="tb">
<tr>
<th style="width:4%;">序号</th>
<th>账号</th>
<th>姓名</th>
<th>密码</th>
<th>权限</th>
<th>电话</th>
<th>邮件</th>
<th>年龄</th>
<th>性别</th>
<th>地址</th>
</tr>
<%for(int i=0;i<10;i++) {%>
<tr>
<td><%=i+1 %></td>
<td><input name="userAccount" id="account<%=i%>"/></td>
<td><input name="userName"/></td>
<td><input name="userPassword"/></td>
<td><input name="userRight"/></td>
<td><input name="userPhone"/></td>
<td><input name="userEmail"/></td>
<td><input name="userAge"/></td>
<td><input name="userSex"/></td>
<td><input name="userAdress"/></td>
</tr>
<%} %>
</table>
</form>
<input type="button" onclick="SubmitAdd()" value="提交" class="submit_btn"/>
<input type="button" onclick="Check()" value="提交" class="submit_btn"/>
</div>
</div>
</div>
</body>
</html>
结果: