edisontu2 2023-03-15 08:57 采纳率: 100%
浏览 92
已结题

使用HTMLD和Javascript做表格的问题

用html和JavaScript做了一个表格
如下

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
</head>

<table id="table1">
    <thead>
        <tr>
            <th style="background-color:#C6E0B4;">選択<input type="checkbox" id="select-all" name="select-all"></th>
            <th style="background-color:#C6E0B4;">氏名</th>
            <th style="background-color:#C6E0B4;">性別</th>
            <th style="background-color:#C6E0B4;">住民税</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" class="select-row" name="select-row"></td>
            <td>张三</td>
            <td>
                <label><input type="radio" name="gender1" value="男性">男性</label>
                <label><input type="radio" name="gender1" value="女性">女性</label>
            </td>
            <td><input type="number" id="a" name="consumption-tax" value="12000"></td>
        </tr>
        <tr>
            <td><input type="checkbox" class="select-row" name="select-row"></td>
            <td>李四</td>
            <td>
                <label><input type="radio" name="gender2" value="男性">男性</label>
                <label><input type="radio" name="gender2" value="女性">女性</label>
            </td>
            <td><input type="number" id="b" name="consumption-tax" value="22098"></td>
        </tr>
        <tr>
            <td><input type="checkbox" class="select-row" name="select-row"></td>
            <td>王五</td>
            <td>
                <label><input type="radio" name="gender3" value="男性">男性</label>
                <label><input type="radio" name="gender3" value="女性">女性</label>
            </td>
            <td><input type="number" id="c" name="consumption-tax" value="2200"></td>
        </tr>
    </tbody>
</table>

<hr>

<tbody>
    <div>
        <p id="left">氏名</p>
        <p class="right"><input class="input1" id="namae" type="text" style="width: 100%; height: 100%;"></p>
        <input class="input2" type="button" name="" id="button" value="追加" style="width: 60%;"
            onclick='addRow()'></input>
    </div>
</tbody>

<hr>

<input class="input3" type="button" name="" id="button2" value="削除" style="width: 10%;" onclick='deleteRows()'></input>

<hr>

<table>
    <tbody>
        <tr>
            <td style="background-color:#C6E0B4;">合計</td>
            <td><input type="number" id="d" name="consumption-tax" value="36298"></td>
        </tr>
        <tr>
            <td style="background-color:#C6E0B4;">男性合計</td>
            <td><input type="number" id="e" name="consumption-tax" value="34098"></td>
        </tr>
        <tr>
            <td style="background-color:#C6E0B4;">女性合計</td>
            <td><input type="number" id="f" name="consumption-tax" value="2000"></td>
        </tr>
    </tbody>
</table>

<script>
    // 全部選択
    const selectAllCheckbox = document.getElementById('select-all');
    const selectRowCheckboxes = document.querySelectorAll('.select-row');

    selectAllCheckbox.addEventListener('click', () => {
        selectRowCheckboxes.forEach((checkbox) => {
            checkbox.checked = selectAllCheckbox.checked;
        });
    });
    //合計
    const inputA = document.getElementById('a');
    const inputB = document.getElementById('b');
    const inputC = document.getElementById('c');
    const inputE = document.getElementById('d');

    function updateTotal() {
        const total = Number(inputA.value) + Number(inputB.value) + Number(inputC.value);
        inputE.value = total;
    }

    inputA.addEventListener('input', updateTotal);
    inputB.addEventListener('input', updateTotal);
    inputC.addEventListener('input', updateTotal);
    //男性の合計
    const genderRadioButtons = document.querySelectorAll('input[name^="gender"]');

    function updateMaleTotal() {
        let maleTotal = 0;
        genderRadioButtons.forEach((radioButton) => {
            if (radioButton.checked && radioButton.value === "男性") {
                const row = radioButton.closest('tr');
                const residentTaxInput = row.querySelector('input[name="consumption-tax"]');
                maleTotal += Number(residentTaxInput.value);
            }
        });
        document.getElementById('e').value = maleTotal;
    }

    genderRadioButtons.forEach((radioButton) => {
        radioButton.addEventListener('click', updateMaleTotal);
    });
    //女性選択時女性合計
    function updateFemaleTotal() {
        let femaleTotal = 0;
        genderRadioButtons.forEach((radioButton) => {
            if (radioButton.checked && radioButton.value === "女性") {
                const row = radioButton.closest('tr');
                const residentTaxInput = row.querySelector('input[name="consumption-tax"]');
                femaleTotal += Number(residentTaxInput.value);
            }
        });
        document.getElementById('f').value = femaleTotal;
    }

    genderRadioButtons.forEach((radioButton) => {
        radioButton.addEventListener('click', () => {
            updateMaleTotal();
            updateFemaleTotal();
        });
    });
    //行追加
    function addRow() {
        var table = document.getElementById("table1");

        var newRow = table.insertRow();

        var row1 = table.rows[1];
        for (var i = 0; i < row1.cells.length; i++) {
            var newCell = newRow.insertCell(i);
            newCell.innerHTML = row1.cells[i].innerHTML;
        }
        input.addEventListener('input', updateTotal);
    }
    //追加行INPUT代入
    function addRow() {
        const table = document.getElementById('table1').getElementsByTagName('tbody')[0];
        const newRow = table.insertRow(-1);
        const checkBoxCell = newRow.insertCell(0);
        checkBoxCell.innerHTML = '<input type="checkbox" class="select-row" name="select-row">';
        const nameCell = newRow.insertCell(1);
        nameCell.textContent = document.getElementById('namae').value;
        const genderCell = newRow.insertCell(2);
        genderCell.innerHTML = '<label><input type="radio" name="gender" value="男性">男性</label><label><input type="radio" name="gender" value="女性">女性</label>';
        const taxCell = newRow.insertCell(3);
        taxCell.innerHTML = '<input type="number" name="consumption-tax" value="0">';
    }
    //CHECKBOX選択行削除    
    function deleteRows() {
        var table = document.getElementById("table1");
        var checkboxes = table.getElementsByTagName("input");
        var selectedRows = [];
        for (var i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].type == "checkbox" && checkboxes[i].checked) {
                selectedRows.push(checkboxes[i].parentNode.parentNode);
            }
        }
        for (var i = 0; i < selectedRows.length; i++) {
            table.deleteRow(selectedRows[i].rowIndex);
        }
    }
    

</script>

<style>
    table {
        border-collapse: collapse;
    }

    th,
    td {
        border: 1px solid black;
        padding: 8px;
    }

    .table1 {
        border-collapse: collapse;
    }

    #left {
        width: 50%;
        border: 1px solid black;
        background-color: #C6E0B4;
        padding: 10px;
    }

    .right {
        width: 50%;
        border: 1px solid black;
        background-color: white;
        padding: 10px;
    }

    div {
        display: flex;
        width: 30%;
    }

    .input1 {
        border: none;
    }

    .input2 {
        margin: 10px;
        padding: 20px;
        background-color: #4472C4;
    }

    .input3 {
        margin: auto 0;
        background-color: red;
        border: none
    }
</style>

</html>

提问!!
怎么样能够实现点击了追加按键,追加的一行也能实现被全选,追加一行的数字也能计入合计。
并且按下男性或者女性的选择按键,下面的男性女性的合计也会改变。

  • 写回答

6条回答 默认 最新

  • 简效 2023-03-15 10:11
    关注

    修改代码如下,多解决了新增多次次性别选择按钮导致name相等的问题

    <!DOCTYPE html>
    <html>
     
    <head>
        <meta charset="UTF-8">
    </head>
    <table id="table1">
        <thead>
            <tr>
                <th style="background-color:#C6E0B4;">選択<input type="checkbox" id="select-all" name="select-all"></th>
                <th style="background-color:#C6E0B4;">氏名</th>
                <th style="background-color:#C6E0B4;">性別</th>
                <th style="background-color:#C6E0B4;">住民税</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="select-row" name="select-row"></td>
                <td>张三</td>
                <td>
                    <label><input type="radio" name="gender1" value="男性">男性</label>
                    <label><input type="radio" name="gender1" value="女性">女性</label>
                </td>
                <td><input type="number" id="a" name="consumption-tax" value="12000"></td>
            </tr>
            <tr>
                <td><input type="checkbox" class="select-row" name="select-row"></td>
                <td>李四</td>
                <td>
                    <label><input type="radio" name="gender2" value="男性">男性</label>
                    <label><input type="radio" name="gender2" value="女性">女性</label>
                </td>
                <td><input type="number" id="b" name="consumption-tax" value="22098"></td>
            </tr>
            <tr>
                <td><input type="checkbox" class="select-row" name="select-row"></td>
                <td>王五</td>
                <td>
                    <label><input type="radio" name="gender3" value="男性">男性</label>
                    <label><input type="radio" name="gender3" value="女性">女性</label>
                </td>
                <td><input type="number" id="c" name="consumption-tax" value="2200"></td>
            </tr>
        </tbody>
    </table>
    <hr>
    <tbody>
        <div>
            <p id="left">氏名</p>
            <p class="right"><input class="input1" id="namae" type="text" style="width: 100%; height: 100%;"></p>
            <input class="input2" type="button" name="" id="button" value="追加" style="width: 60%;"
                onclick='addRow()'></input>
        </div>
    </tbody>
    <hr>
    <input class="input3" type="button" name="" id="button2" value="削除" style="width: 10%;" onclick='deleteRows()'></input>
    <hr>
    <table>
        <tbody>
            <tr>
                <td style="background-color:#C6E0B4;">合計</td>
                <td><input type="number" id="d" name="consumption-tax" value="36298"></td>
            </tr>
            <tr>
                <td style="background-color:#C6E0B4;">男性合計</td>
                <td><input type="number" id="e" name="consumption-tax" value="34098"></td>
            </tr>
            <tr>
                <td style="background-color:#C6E0B4;">女性合計</td>
                <td><input type="number" id="f" name="consumption-tax" value="2000"></td>
            </tr>
        </tbody>
    </table>
     
    <script>
        // 全部選択
        const selectAllCheckbox = document.getElementById('select-all');
        
        selectAllCheckbox.addEventListener('click', () => {
            const selectRowCheckboxes = document.querySelectorAll('.select-row');
            selectRowCheckboxes.forEach((checkbox) => {
                checkbox.checked = selectAllCheckbox.checked;
            });
        });
        //合計
        const inputA = document.getElementById('a');
        const inputB = document.getElementById('b');
        const inputC = document.getElementById('c');
        const inputE = document.getElementById('d');
     
        function updateTotal() {
            const tables = document.getElementById("table1").querySelectorAll('input[name="consumption-tax"]')
            let total = 0;
            tables.forEach((v,i)=>{
                total+=Number(v.value)
            })
            inputE.value = total;
        }
     
        inputA.addEventListener('input', updateTotal);
        inputB.addEventListener('input', updateTotal);
        inputC.addEventListener('input', updateTotal);
        //男性の合計
        
        let genderRadioButtons = document.querySelectorAll('input[name^="gender"]');
     
        function updateMaleTotal() {
            let maleTotal = 0;
            genderRadioButtons.forEach((radioButton) => {
                if (radioButton.checked && radioButton.value === "男性") {
                    const row = radioButton.closest('tr');
                    const residentTaxInput = row.querySelector('input[name="consumption-tax"]');
                    maleTotal += Number(residentTaxInput.value);
                }
            });
            document.getElementById('e').value = maleTotal;
        }
     
        genderRadioButtons.forEach((radioButton) => {
            radioButton.addEventListener('click', updateMaleTotal);
        });
        //女性選択時女性合計
        function updateFemaleTotal() {
            let femaleTotal = 0;
            genderRadioButtons.forEach((radioButton) => {
                if (radioButton.checked && radioButton.value === "女性") {
                    const row = radioButton.closest('tr');
                    const residentTaxInput = row.querySelector('input[name="consumption-tax"]');
                    femaleTotal += Number(residentTaxInput.value);
                }
            });
            document.getElementById('f').value = femaleTotal;
        }
     
        genderRadioButtons.forEach((radioButton) => {
            radioButton.addEventListener('click', () => {
                updateMaleTotal();
                updateFemaleTotal();
            });
        });
        //行追加
        function addRow() {
            var table = document.getElementById("table1");
     
            var newRow = table.insertRow();
     
            var row1 = table.rows[1];
            for (var i = 0; i < row1.cells.length; i++) {
                var newCell = newRow.insertCell(i);
                newCell.innerHTML = row1.cells[i].innerHTML;
            }
            input.addEventListener('input', updateTotal);
        }
        //追加行INPUT代入
        let gender = 4
        function addRow() {
            const table = document.getElementById('table1').getElementsByTagName('tbody')[0];
            const newRow = table.insertRow(-1);
            const checkBoxCell = newRow.insertCell(0);
            checkBoxCell.innerHTML = '<input type="checkbox" class="select-row" name="select-row">';
            const nameCell = newRow.insertCell(1);
            nameCell.textContent = document.getElementById('namae').value;
            const genderCell = newRow.insertCell(2);
            genderCell.innerHTML = `<label><input type="radio" name="gender${gender}" value="男性">男性</label><label><input type="radio" name="gender${gender}" value="女性">女性</label>`;
            gender++
            const taxCell = newRow.insertCell(3);
            taxCell.innerHTML = '<input type="number" oninput="updateTotal()" name="consumption-tax" value="0">';
            genderRadioButtons = document.querySelectorAll('input[name^="gender"]');
            genderRadioButtons.forEach((radioButton) => {
                radioButton.addEventListener('click', () => {
                    updateMaleTotal();
                    updateFemaleTotal();
                });
            });
        }
        //CHECKBOX選択行削除    
        function deleteRows() {
            var table = document.getElementById("table1");
            var checkboxes = table.getElementsByTagName("input");
            var selectedRows = [];
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].type == "checkbox" && checkboxes[i].checked) {
                    selectedRows.push(checkboxes[i].parentNode.parentNode);
                }
            }
            for (var i = 0; i < selectedRows.length; i++) {
                table.deleteRow(selectedRows[i].rowIndex);
            }
        }
        
     
    </script>
     
    <style>
        table {
            border-collapse: collapse;
        }
     
        th,
        td {
            border: 1px solid black;
            padding: 8px;
        }
     
        .table1 {
            border-collapse: collapse;
        }
     
        #left {
            width: 50%;
            border: 1px solid black;
            background-color: #C6E0B4;
            padding: 10px;
        }
     
        .right {
            width: 50%;
            border: 1px solid black;
            background-color: white;
            padding: 10px;
        }
     
        div {
            display: flex;
            width: 30%;
        }
     
        .input1 {
            border: none;
        }
     
        .input2 {
            margin: 10px;
            padding: 20px;
            background-color: #4472C4;
        }
     
        .input3 {
            margin: auto 0;
            background-color: red;
            border: none
        }
    </style>
     
    </html>
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月15日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行