<%--
Created by IntelliJ IDEA.
User: goudiyuan
Date: 2023/5/31
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>选择学生</title>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-3.6.0.js"></script>
</head>
<body>
您好:${ USER_SESSION.name }
<a href="${ pageContext.request.contextPath }/logout">退出</a>
<form id="students" action="${pageContext.request.contextPath }/getStudents" method="post">
<table width="220px" border="1">
<tr>
<td>选择</td>
<td>id</td>
<td>姓名</td>
<td>班级</td>
</tr>
<tr>
<td>
<input name="students" value="1" type="checkbox">
</td>
<td>
<input name="id" value="1" id="id" type="text">
</td>
<td>
<input name="name" value="张三" id="name" type="text">
</td>
<td>
<input name="cid" value="1" id="cid" type="text">
</td>
</tr>
<tr>
<td>
<input name="students" value="2" type="checkbox">
</td>
<td>
<input name="id" value="2" id="id2" type="text">
</td>
<td>
<input name="name" value="李四" id="name2" type="text">
</td>
<td>
<input name="cid" value="2" id="cid2" type="text">
</td>
</tr>
<tr>
<td>
<input name="students" value="3" type="checkbox">
</td>
<td>
<input name="id" value="3" id="id3" type="text">
</td>
<td>
<input name="name" value="王五" id="name3" type="text">
</td>
<td>
<input name="cid" value="1" id="cid3" type="text">
</td>
</tr>
<tr>
<td>
<input name="students" value="4" type="checkbox">
</td>
<td>
<input name="id" value="4" id="id4" type="text">
</td>
<td>
<input name="name" value="赵六" id="name4" type="text">
</td>
<td>
<input name="cid" value="2" id="cid4" type="text">
</td>
</tr>
</table>
<input type="submit" value="提交学生" onclick="submitStudents()"/>
</form>
<script type="text/javascript">
function submitStudents() {
var stu1 = {id: $("#id").val(), name: $("#name").val(), cid:$("#cid").val()}
var stu2 = {id: $("#id2").val(), name: $("#name2").val(), cid:$("#cid2").val()}
var stu3 = {id: $("#id3").val(), name: $("#name3").val(), cid:$("#cid3").val()}
var stu4 = {id: $("#id4").val(), name: $("#name4").val(), cid:$("#cid4").val()}
$.ajax({
url: "${pageContext.request.contextPath }/getStudentList",
type: "post",
data: JSON.stringify([stu1,stu2,stu3,stu4]),
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
}
</script>
</body>
</html>
如上面代码是我在java ee中写的想要通过json数据绑定返回我选中的值,如图:
但是由于这段代码:
data: JSON.stringify([stu1,stu2,stu3,stu4])
他就自动每次都返回了所有的值
如何做到只返回我想要的值呢?需要我把选中的值再拿来重新做一个表格。