简体   繁体   English

如何在表格中创建复选框列?

[英]How to create checkbox column in the table?

I am dynamically creating a table using JavaScript code below: 我正在使用以下JavaScript代码动态创建表:

function CreatTable(data) {

    var tablearea;
    var table;
    var thead;
    var tr;
    var th;

    tablearea = document.getElementById('ShowDataID');
    table = document.createElement('table');
    thead = document.createElement('thead');
    tr = document.createElement('tr');

    for (var i = 0; i < data.length; i++) {
        var headerTxt = document.createTextNode(data[i]);
        th = document.createElement('th');
        th.appendChild(headerTxt);
        tr.appendChild(th);
        thead.appendChild(tr);
    }

    table.appendChild(thead);

    for (var i = 1; i < 4; i++) {
        tr = document.createElement('tr');
        tr.appendChild(document.createElement('td'));
        tr.appendChild(document.createElement('td'));
        tr.appendChild(document.createElement('td'));
        tr.cells[0].appendChild(document.createTextNode('John'));
        tr.cells[1].appendChild(document.createTextNode('McDowell'));
        tr.cells[2].appendChild(document.createTextNode('ddd@gmail.com'));

        table.appendChild(tr);
    }
    tablearea.appendChild(table);
}
</script>

When I create table I also need to create checkbox column in the table above. 当我创建表时,我还需要在上表中创建复选框列。

Any idea how I can implement it using JavaScript? 知道如何使用JavaScript实现它吗? Or related link? 还是相关的链接?

I'm not sure if this is what you're asking: 我不确定这是不是你问的问题:

var checkbox = document.createElement("INPUT");
checkbox.type = "checkbox";

and then you'd append checkbox to each row in order to form a new column. 然后你会在每一行附加checkbox以形成一个新列。

See this fiddle: http://jsfiddle.net/qeeu18g1/3/ I added comments where I added things. 看到这个小提琴: http//jsfiddle.net/qeeu18g1/3/我在添加内容的地方添加了评论。

(is this a duplicate of this question ?) (这是这个问题的副本吗?)

Use the following JS code: 使用以下JS代码:

var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("name", "city");
x.setAttribute("value", "London");
x.setAttribute("id", 1);

And place it whereever you want to add it. 并将其放置在您想要添加它的位置。

See more at HTML DOM Input Checkbox Object 有关详细信息,请参阅HTML DOM输入复选框对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM