情况:我要批量插入表格里的数据,使用复选框,选中复选框时,怎么把该复选框的行的数据也取出来。
像这样,我要取出标题,作者等信息,咋取?在这记一下。
jsp页面,我的表格是酱的,我要取fid,title,userid
<table class="layui-table" lay-skin="line">
<colgroup>
</colgroup>
<thead>
<tr>
<th><input type="checkbox" name="checkbox_all" id="checkbox_all" lay-skin="primary">全选</th>
<th>标题</th>
<th>作者</th>
<th>阅读数</th>
<th>获赞数</th>
<th>收藏数</th>
<th>评论数</th>
</tr>
</thead>
<tbody>
<tr>
<c:forEach items="${allArticles}" var="allArticles" end="10">
<td><input type="checkbox" name="checkbox_name" value="${allArticles.fid}" lay-skin="primary"></td>
<td>
<input hidden value="${allArticles.title}" name="title">
<a href="showArticle?fid=${allArticles.fid}&&userid=${allArticles.userid}">${allArticles.title}</a>
</td>
<td><input hidden value="${allArticles.userid}" name="userid">${allArticles.username}</td>
<td>${allArticles.readNum}</td>
<td>${allArticles.likes}</td>
<td>${allArticles.collectNum}</td>
<td>${allArticles.commentNum}</td>
</tr>
</c:forEach>
</tbody>
</table>
脚本:
let oid = document.getElementsByName("checkbox_name");
$('.submit_btn').on('click', function () {
let ids = "";
let userids = "";
let titles = "";
let n = 0;
let a = 0;
let b = 0;
for (let i = 0; i < oid.length; i++) {
if (oid[i].checked == true) {//选中为true
const row = $(oid[i]).parent("td").parent("tr");//获取选中行
const userid = row.find("[name='userid']").val();//获取userid
const title = row.find("[name='title']").val();//获取title
let id = oid[i].value;
if (n == 0) {
ids += id;
} else {
ids += "," + id;
}
if (a == 0) {
userids += userid;
} else {
userids += "," + userid;
}
if (b == 0) {
titles += title;
} else {
titles += "," + title;
}
b++;
n++;
a++;
}
}
// const row = $(oid).parent("td").parent("tr");//获取选中行
// const userid = row.find("[name='userid']").val();//获取userid
// alert(userids + "userid");
if (!ids) {
layer.alert('选中之后再点击确定啦');
} else {
let data = {
ids: ids,
userids: userids,
titles: titles,
label: $('input:radio:checked').val()
};
$.ajax({
type: "Post",
url: "insert_discover",
// contentType: "charset=utf-8",
data: data,
dataType: "json",
success: function (result) {
if (result.resultCode == 200) {
layer.msg('操作成功!');
}
if (result.resultCode == 404) {
layer.msg('操作失败!');
}
setTimeout(function () {
window.location.reload();//刷新当前页面.
}, 2000)
},
error: function (data) {
layer.alert('失败');
}
});
}
}
)
存进数组,后面接受,那么我要怎么把这些数据存入数据库?我要写在另一篇,两个不同的知识点,我分开写,以后好找。