多个input的name属性值相同,遍历取值

分别通过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>

结果:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值