前述:终于抽出时间,整理了一些编程所需基础知识。

        js遍历json数据再添加到table中。选择所需行后点击提交拿到id传给后台,(也可以放入集合一起给)。

先看效果:

选择点击后:

code:


<body>

<div id="tb"></div>
<button onclick="submit()">提交</button>
<script>
    var jsonstr = '[{"id":"01sdfae24lj0s34k4234kijwerm3", info0: "01", "info2": "梨子", "info3": "3个"},' +
        '{"id":"02sdfae24lj0s66d32t6gu9jwerm3", "info0": "02", "info2": "苹果", "info3": "3个"},' +
        '{"id":"03sdfae24lj0s348t53f78tty843","info0": "03","info2": "香蕉", "info3": "6个"},' +
        '{"id":"04sdfae24lj0s34k4kd45gtrrm3","info0": "04","info2": "橘子", "info3": "6个"}]';

    function tablef() {
        var jsonData = new Function("return " + jsonstr)();
        var table = document.createElement("table");
        table.className = "table";
        table.id = "table";
        htmls = "<tr>" +
            "<td style='width:25px;'></td>" +
            "<td style='width:50px;'>序号</td>" +
            "<td style='width:100px;'>名称</td>" +
            "<td style='width:100px;'>个数</td>" +
            "</tr>";
        table.innerHTML = htmls;
        for (i = 0; i < jsonData.length; i++) {
            var trs = document.createElement("tr");
            var v = i + 1;
            var inbx = "<td><input class='checkbox-css'  id='cbx_" + v + "' value='' type='checkbox'/></td>";
            trs.innerHTML = inbx;
            for (tdvalue in jsonData[i]) {
                var td = document.createElement("td");
                td.appendChild(document.createTextNode(jsonData[i][tdvalue]));
                if (tdvalue == "id") {
                    td.className = "disno";
                }
                trs.appendChild(td);
            }
            table.appendChild(trs);
        }
        document.getElementById("tb").appendChild(table);
    }

    window.onload = function () {
        tablef();
    };

    //submit
    function submit() {
        var table = document.getElementById('table');
        for (i = 1; i < table.rows.length; i++) {
            var id = document.getElementById("cbx_" + i);
            if (id.checked) {
                console.log(table.rows[i].cells[1].innerHTML);
            }
        }
    }
</script>


</body>

所需小知识:

1、js创建table:

<table id="tb"></table>
<script>
    var oTab = document.getElementById('tb');
    trHtml = "<tr>" +
        "<th style='width: 6%;'></th>" +
        "<th style='width: 6%;'>1</th>" +
        "</tr>";
    oTab.innerHTML = trHtml;
    for (var i = 0; i < 4; i++) {
        var oTr = document.createElement('tr');
        var inbx="<td><input class='checkbox-css'  id='itcheckbox' type='checkbox'/></td>";
        oTr.innerHTML=inbx;
        for (var j = 0; j < 4; j++) {
            var oTd = document.createElement('td');
            oTr.appendChild(oTd);
        }
        oTab.appendChild(oTr);
    }
</script>

2、table样式:

合并线条: border-collapse: collapse;
文字居中: text-align: center;
文字颜色: color: #666;
添加框线: border: 1px solid #caf1c5;
背景颜色: background-color: #CCE8EB;
整体背景色:background: #fff;

标签隐藏:display: none;

整体css:

<style type="text/css">
    table
    {
        border-collapse: collapse;
        text-align: center;
    }
    table td, table th
    {
        border: 1px solid #caf1c5;
        color: #666;
        height: 30px;
    }
    table thead th
    {
        background-color: #CCE8EB;
        width: 100px;
    }
    table tr:nth-child(odd)
    {
        background: #fff;
    }
    table tr:nth-child(even)
    {
        background: #f1f3fd;
    }


</style>

3、js基础知识:https://blog.csdn.net/xysxlgq/article/details/119570069

GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐