如何在使用javascript创建表时创建复选框?

如何在使用javascript创建表时创建复选框?,javascript,jquery,Javascript,Jquery,我在页面中创建了一个带有javascript函数的html表。我需要在表中每行最后一列的中创建一个复选框。 我不知道怎么做。 有人能帮我吗?请给我举个例子 这是我创建表的代码 $(文档).ready(函数(){ $(“#提交文件”)。在(“单击”上,函数(e){ if($('#files').val()=“”){ 警报(“安达必须遵守文件规定”); }否则{ e、 预防默认值(); $(“#文件”).parse({ 配置:{ 分隔符:“”, skipEmptyLines:错误, 完成:disp

我在页面中创建了一个带有
javascript
函数的html表。我需要在表中每行最后一列的
中创建一个
复选框。
我不知道怎么做。
有人能帮我吗?请给我举个例子

这是我创建表的代码

$(文档).ready(函数(){
$(“#提交文件”)。在(“单击”上,函数(e){
if($('#files').val()=“”){
警报(“安达必须遵守文件规定”);
}否则{
e、 预防默认值();
$(“#文件”).parse({
配置:{
分隔符:“”,
skipEmptyLines:错误,
完成:displayHTMLTable,
},
前:函数(文件、inputElem){
//log(“解析文件…”,文件);
},
错误:函数(错误,文件){
//log(“错误:”,错误,文件);
},
完成:函数(){
//log(“处理完所有文件”);
}
});
}
});
函数displayHTMLTable(结果){
var表=”;
var数据=结果数据;
变量大小=-1;
对于(i=1;isize)size=cells.length,则为else;
对于(j=0;j

上载文件数据:
上载文件

我刚刚添加了一点,您可以尝试:

function displayHTMLTable(results) {
    var table = "<table class='table table-bordered'>";
    var data = results.data;
    var size = -1;
    var header = "<thead><tr>";
    header+= "<th>Column header 1</th>";
    header+= "<th>Column header 2</th>";
    header+= "<th>Column header 3</th>";
    header+= "<th>Column header 4</th>";
    header+= "<th>Column header for checkbox</th>";
    header+= "</tr></thead>";
    table += header;
    table+="<tbody>";
    for (i = 1; i < data.length; i++) {
        table += "<tr>";
        var row = data[i];
        var cells = row.join(",").split(",");
        if (cells.length < size) continue;
        else if (cells.length > size) size = cells.length;
        for (j = 0; j < cells.length; j++) {

            table += "<td>";
            table += cells[j];
            table += "</td>";
        }
        table += "<td><input type='checkbox' name='mycheckox'></td>"
        table += "</tr>";
    }
    table+="</tbody>";
    table += "</table>";
    $("#parsed_csv_list").html(table);
}
函数显示HTMLTable(结果){
var表=”;
var数据=结果数据;
变量大小=-1;
var header=“”;
标题+=“列标题1”;
标题+=“列标题2”;
标题+=“列标题3”;
标题+=“列标题4”;
标题+=“复选框的列标题”;
标题+=“”;
表+=标题;
表+=”;
对于(i=1;isize)size=cells.length,则为else;
对于(j=0;j
函数显示HTMLTable(结果){
const table=document.createElement('table');
table.className='table-table-bordered';
//添加thead。
const thead=table.appendChild(document.createElement('thead');
const theadRow=thead.appendChild(document.createElement('tr');
//假设第一行是带有标题的行。
results.data[0]。拆分(',).forEach((头)=>{
theadRow.appendChild(document.createElement('th')).innerText=标题;
});
//为复选框列添加空th。
theadRow.appendChild(document.createElement('th'));
//添加tbody。
const tbody=table.appendChild(document.createElement('tbody');
results.data.forEach((行,i)=>{
如果(i==0){
回来
}
const tr=tbody.appendChild(document.createElement('tr');
常量单元格=行分割(',');
cells.forEach((cell)=>{
tr.appendChild(document.createElement('td')).innerText=cell;
});
//在这里添加复选框。
const td=tr.appendChild(document.createElement('td'));
const chk=td.appendChild(document.createElement('input');
chk.type='复选框';
//设置复选框的值、名称等。
//chk.value=单元格[0];
//chk.name=`chk-${cells[0]}`;
});
document.getElementById('parsed_csv_list').appendChild(表);
}
常数结果={
数据:[
“身份证、姓名、年龄”,
“1号,无名氏,25号”,
“2号,简·多伊,20号”,
“3,玛丽·简,30”,
“4号,约翰·史密斯,35号”,
],
};
显示HTMLTable(结果)
*{
保证金:0;
大纲:0;
填充:0;
}
html,正文{
字体:标准14px/1.8 Helvetica,Arial,无衬线;
}
桌子{
边界塌陷:塌陷;
利润率:10px0;
表布局:固定;
}
th,td{
边框:1px实心#ccc;
填充物:5px;
}
th{
背景:#ccc;
}

您的代码段显示了一个错误:
语法错误:预期表达式,得到“}”
您有一个额外的
})在结尾处与任何内容都不匹配。嗯。。不知道为什么我用它来制作html表bro在创建表之前我需要先发布代码吗?
var cells=row.join(“,”).split(“,”)为什么不仅仅是
var cells=row
?我已经编辑了我的代码。。我把我所有的代码都写在这里了。。当我需要在那里做
th
时,如何做?想在表格的第一行添加标题行吗?@RobSutan请查看我的答案以回答您的问题。@tranadi是的。。我想在其中添加一个thead好的,你确定列数了吗?标题是硬修复还是从数据中生成?您的代码在我的方法中不起作用。因为在创建表orry的代码之前,数据需要知道我函数中的数组解析,所以我不理解。你能给出一个样本数据吗?我没有样本数据。因为数据来自csv文件。你可以看我的问题代码。从那以后。我有一个创建表的函数。不,从@Tran Audi已回答的函数中,我需要在
表+=cells[j]
更新我的answer.thx之前创建一个
thead
,以获得您的答案。特兰奥迪已经给了我答案。而且它起作用了
function displayHTMLTable(results) {
  const table = document.createElement('table');
  table.className = 'table table-bordered';

  // Add the thead.
  const thead = table.appendChild(document.createElement('thead'));
  const theadRow = thead.appendChild(document.createElement('tr'));

  // Assuming the first row is the line with the headers.
  results.data[0].split(',').forEach((header) => {
    theadRow.appendChild(document.createElement('th')).innerText = header;
  });

  // Add an empty th for the checkbox column.
  theadRow.appendChild(document.createElement('th'));

  // Add the tbody.
  const tbody = table.appendChild(document.createElement('tbody'));

  results.data.forEach((row, i) => {
    if (i === 0) {
      return;
    }

    const tr = tbody.appendChild(document.createElement('tr'));
    const cells = row.split(',');
    cells.forEach((cell) => {
      tr.appendChild(document.createElement('td')).innerText = cell;
    });

    // Add the checkbox here.
    const td = tr.appendChild(document.createElement('td'));
    const chk = td.appendChild(document.createElement('input'));
    chk.type = 'checkbox';
    // Set the value, name, etc. of the checkbox.
    // chk.value = cells[0];
    // chk.name = `chk-${cells[0]}`;
  });

  document.getElementById('parsed_csv_list').appendChild(table);
}