javascript - 从表附加到另一个表时添加额外的列

标签 javascript jquery html

enter image description here在这里,我尝试将一个表(3 列)附加到另一个表。效果很好。我需要一些额外的列。谁能帮帮我吗。我的片段。

function table2(){
 $('#one').on("click", function(){
    $('#one tbody input:checked').parent().parent().appendTo("#two");
 } 

最佳答案

这可能会有所帮助,这是一个示例。

// First of all this is an example, you has to modify this make as you wish
// Here we can add dynamic columns & rows
// Also , I have given an option to specify the row/column name
<table border="1" id="mtable">
    <thead>
        <tr>
            <td>Item</td><td>Red</td><td>Green</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Some Item</td><td><input type="checkbox"/></td><td><input type="checkbox"/></td>
        </tr>
    </tbody>
</table>
<br/><br/>
<input id="row" placeholder="Enter Item Name"/><button id="irow">Insert Row</button><br/><br/>
<input id="col" placeholder="Enter Heading"/><button id="icol">Insert Column</button>

<script>
    // This is add additional row to a table
    $('#irow').click(function(){
        if($('#row').val()){
            // Creates a copy of last row and you specify the name of row as well as
            // each <td>. Here I'm given a checkbox.
            $('#mtable tbody').append($("#mtable tbody tr:last").clone());
            $('#mtable tbody tr:last :checkbox').attr('checked',false);
            $('#mtable tbody tr:last td:first').html($('#row').val());
        }else{alert('Enter Text');}
    });
    $('#icol').click(function(){
        // This is add additional column to a table
        if($('#col').val()){
            // This is to append/add column to a table
            $('#mtable tr').append($("<td>"));
            $('#mtable thead tr>td:last').html($('#col').val());
            // This is to add a column to each rows in a table
            $('#mtable tbody tr').each(function(){
                $(this).children('td:last').append($('<input type="checkbox">'));
            });
        } else { alert('Enter Text'); }
    });
</script>

关于javascript - 从表附加到另一个表时添加额外的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962478/

相关文章:

javascript - 如何在javascript中将字节数组转换为图像

mysql - 使用解析器将 HTML 内容解析到 MySQL 数据库中

javascript - 错误: Tried to register widget with id==class_level_grid but that id is already registered in DOJO

Javascript函数问题

javascript - 在某些 REST API 上有选择地启用 HTTP 基本身份验证

javascript - 如何通过用户输入更改按钮值

php - 如何将变量从 JavaScript 传递到 PHP?

javascript - 如何访问元素相对于其父元素的位置

javascript - google.maps.event.addDomListener() 已弃用,请改用标准 addEventListener() 方法 : Google Place autocomplete Error

javascript - 设置 Canvas 背景颜色