Javascript 执行单击(检查显示)操作后,再次执行第二次单击(检查消失)操作。需要时,再次单击以删除第一个操作

Javascript 执行单击(检查显示)操作后,再次执行第二次单击(检查消失)操作。需要时,再次单击以删除第一个操作,javascript,function,Javascript,Function,我已经修改了你的密码,这是密码 HTML 我将输入元素更改为调用updateTable(),并传递一个额外的参数this "use strict"; var t_names = ["1table-body", "2table-body", "3table-body", "4table-body"]; var counter = [0, 0, 0, 0]; function addRow(v_id, t_id) { var t_string = t_names[t_id]; c

我已经修改了你的密码,这是密码

HTML

我将输入元素更改为调用updateTable(),并传递一个额外的参数this

 "use strict";
var t_names = ["1table-body", "2table-body", "3table-body", "4table-body"];
var counter = [0, 0, 0, 0];

function addRow(v_id, t_id) {
    var t_string = t_names[t_id];
    console.log(t_string)
    var tableBody = document.getElementById(t_string);
    //find last row
    var row = tableBody.rows[tableBody.rows.length - 1];
    //if the row has six cells or there are no rows create a new one
    if (row === null || row === undefined || row.cells.length >= 6) {
        row = document.createElement("tr");
        tableBody.appendChild(row);
    }
    //create new cell
    var th = document.createElement("th");
    //add data to cell
    th.innerHTML = document.getElementById(v_id).innerHTML;
    //add cell to row
    row.appendChild(th);
}

我已经修改了你的密码,这是密码

HTML

我将输入元素更改为调用updateTable(),并传递一个额外的参数this

 "use strict";
var t_names = ["1table-body", "2table-body", "3table-body", "4table-body"];
var counter = [0, 0, 0, 0];

function addRow(v_id, t_id) {
    var t_string = t_names[t_id];
    console.log(t_string)
    var tableBody = document.getElementById(t_string);
    //find last row
    var row = tableBody.rows[tableBody.rows.length - 1];
    //if the row has six cells or there are no rows create a new one
    if (row === null || row === undefined || row.cells.length >= 6) {
        row = document.createElement("tr");
        tableBody.appendChild(row);
    }
    //create new cell
    var th = document.createElement("th");
    //add data to cell
    th.innerHTML = document.getElementById(v_id).innerHTML;
    //add cell to row
    row.appendChild(th);
}

谢谢你的意见,布莱恩。我不是一个程序员,我刚刚开始建立我的Javascript知识。你能帮我把doWork函数合并到addrow函数中吗?@PaulHauer当然可以,只要设置一个fiddle并用代码更新我的答案。@PaulHauer-answer-Updated。请查收。:)谢谢你,布莱恩。它工作得很好。现在的问题是,在完整的应用程序中,有许多复选框指向多个表。我可以用一个变量来代替“chkUpdateTable”吗?或者我可以在(“chkUpdateTable”)中放置多个ID吗?@PaulHauer我想你可能会遇到这个问题。我已经更新了代码和小提琴的链接。它不再使用我以前添加的两个常量。如果有帮助,请告诉我。另外,如果我帮你解决了问题,你能给我一个向上的投票,然后点击我答案左边的复选标记吗:)谢谢你的意见,布莱恩。我不是一个程序员,我刚刚开始建立我的Javascript知识。你能帮我把doWork函数合并到addrow函数中吗?@PaulHauer当然可以,只要设置一个fiddle并用代码更新我的答案。@PaulHauer-answer-Updated。请查收。:)谢谢你,布莱恩。它工作得很好。现在的问题是,在完整的应用程序中,有许多复选框指向多个表。我可以用一个变量来代替“chkUpdateTable”吗?或者我可以在(“chkUpdateTable”)中放置多个ID吗?@PaulHauer我想你可能会遇到这个问题。我已经更新了代码和小提琴的链接。它不再使用我以前添加的两个常量。如果有帮助,请告诉我。另外,如果我帮你解决了你的问题,你能给我一个向上投票并点击我答案左边的复选标记吗:)
 "use strict";
var t_names = ["1table-body", "2table-body", "3table-body", "4table-body"];
var counter = [0, 0, 0, 0];

function addRow(v_id, t_id) {
    var t_string = t_names[t_id];
    console.log(t_string)
    var tableBody = document.getElementById(t_string);
    //find last row
    var row = tableBody.rows[tableBody.rows.length - 1];
    //if the row has six cells or there are no rows create a new one
    if (row === null || row === undefined || row.cells.length >= 6) {
        row = document.createElement("tr");
        tableBody.appendChild(row);
    }
    //create new cell
    var th = document.createElement("th");
    //add data to cell
    th.innerHTML = document.getElementById(v_id).innerHTML;
    //add cell to row
    row.appendChild(th);
}
<body>
    <h1 align="center">Safety Summary Generator</h1>
    <table style="width:">
        <tr>
            <th height="200" width="150">
                <div id="CEC">
                    <img src="../CHEMICAL EXPOSURE - CORROSIVE.png" alt="CHEMICAL EXPOSURE - 
                CORROSIVE" width="100" height="100">
                        <p>CHEMICAL EXPOSURE - CORROSIVE</p>
                    </div>

               <input type="checkbox" value="Load" onClick="updateTable('CEC', 1, this)">
</th>undefined</tr>undefined</table>undefined<h3 align="center">(Safety summary print sheet below)</h3>undefined<div>undefined<h1>
<hr color="#00cc00" width="100%">
</h1>
<div id="printDiv">
    <table id="2table" border="1">
        <h2>Chemical Hazards</h2>
        <thead id="2table-head"></thead>
        <tbody id="2table-body"></tbody>
    </table>
</div>
<h1>
    <hr color="#00cc00" width="100%">
    </h1>
    <div align="center">
        <input type="button" onclick="PSSSReset()" value="PSSS/HWDI Delete">
        </div>
        <br>
            <h3 align="center" style="color: red">Caution: Be sure to review the print sheet (between the 
            green lines) before pressing the Print Safety Summary button. You can only print once before 
            refreshing the browser.</h3>
            <div align="center">
                <button id="doPrint">Print Saftey Summary</button>
            </div>
        </div>
    </body>
var t_names = ["1table-body", "2table-body", "3table-body", "4table-body"];
var counter = [0, 0, 0, 0];


function updateTable(vid, tid, thisCheckbox) {
   var t_string = t_names[tid];
   var tableBody = document.getElementById(t_string);
    //check if table is empty
   if (tableBody.rows.length === 0) {
        //table is empty so regardless of checkbox state
        //checkbox needs to be checked now as we are going
        //to add the row
        thisCheckbox.checked = true;
        addRow(vid, tid);
    } else {
        //table is not empty so make sure checkbox is unchecked
        //then remove the row
        thisCheckbox.checked = false;
        delRow(tableBody);
    }
}
function delRow(tableBody) {
    while (tableBody.rows.length > 0) {
        tableBody.deleteRow(0);
    }
}

function addRow(v_id, t_id) {
    var t_string = t_names[t_id];
    console.log(t_string)
    var tableBody = document.getElementById(t_string);
    //find last row
    var row = tableBody.rows[tableBody.rows.length - 1];
    //if the row has six cells or there are no rows create a new one
    if (row === null || row === undefined || row.cells.length >= 6) {
        row = document.createElement("tr");
        tableBody.appendChild(row);
    }
    //create new cell
    var th = document.createElement("th");
    //add data to cell
    th.innerHTML = document.getElementById(v_id).innerHTML;
    //add cell to row
    row.appendChild(th);
}