Implement the check box and retrieve the row data of the check box

Situation: I want to batch insert the data in the table. Use the check box. When the check box is selected, how can I get the data of the row in the check box.

Like this, I want to get the title, author and other information, how to get it? Remember here.

jsp page, my form is sauce, I want to take 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">All election</th>
                    <th>Title</th>
                    <th>author</th>
                    <th>Reading number</th>
                    <th>Praise number</th>
                    <th>Collection number</th>
                    <th>Comment number</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>

Script:

 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) {//Check to true
                        const row = $(oid[i]).parent("td").parent("tr");//Get selected row
                        const userid = row.find("[name='userid']").val();//Get userid
                        const title = row.find("[name='title']").val();//Get 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"); / / get the selected row
                // Const userid = row. Find ("[name ='userid ']). Val(); / / get userid
                // alert(userids + "userid");
                if (!ids) {
                    layer.alert('Select and click OK');
                } 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('Operation succeeded!');
                            }
                            if (result.resultCode == 404) {
                                layer.msg('Operation failed!');
                            }
                            setTimeout(function () {
                                window.location.reload();//Refresh the current page
                            }, 2000)
                        },
                        error: function (data) {
                            layer.alert('fail');
                        }
                    });
                }

            }
        )

Put it in the array and accept it later. How can I put this data into the database? I want to write in another article, two different knowledge points, I write separately, later easy to find.

132 original articles published, 74 praised, 40000 visitors+
Private letter follow

Tags: JSP JSON Database

Posted on Tue, 17 Mar 2020 08:11:45 -0700 by VapiD