Html 如何在物化表中使用复选框

Html 如何在物化表中使用复选框,html,checkbox,materialize,Html,Checkbox,Materialize,我正在使用Materialize,并试图创建一个包含复选框的表。然而,似乎有一个问题。表中包含的复选框似乎不起作用。我不确定这是一个bug还是我做的事情不正确: <form action="#"> <table> <thead> <tr> <th>Name</th> <th>Status</th> </tr> &l

我正在使用Materialize,并试图创建一个包含复选框的表。然而,似乎有一个问题。表中包含的复选框似乎不起作用。我不确定这是一个bug还是我做的事情不正确:

<form action="#">
  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>My Label</td>
        <td><input type="checkbox" class="filled-in" /><label></label></td>
      </tr>
    </tbody>
  </table>
</form>
如何使用Materialize创建表中包含的复选框

您没有复选框的id,也没有与标签的属性匹配的id:

<input type="checkbox" id="myCheckbox" class="filled-in" />
<label for="myCheckbox"></label>

我已经写了一小段代码,它将对象嵌套数组的数据转换为带有复选框的物化表,因此如果您不想查看任何一列,只需选择或取消选择它

   // Using the Materilize css Check boxes change the functionality of the check bocx accordingly








var tab= document.getElementById("TransformTable");
//Get The No of Cols from the JSON data 
 var NoOfCols=Object.getOwnPropertyNames(data[0]); //Insert the row for the table headings var row = tab.insertRow(0); //set the attribute row.setAttribute("id",0+"_row"); /**  * Loop for No of Cols and set the heading and append the CheckBox  */ for(var i=0;i<NoOfCols.length;i++){ var cell1 =row.insertCell(i); cell1.setAttribute("id","0_row"+i+"_cell"); cell1.innerHTML="<b>"+NoOfCols[i]+"</b>";

    $("#checkBoxDiv").append("<input type='checkbox' id="+i+" checked='checked' style='margin:10px;'/><label for="+i+" style='margin-right:10px;'>"+NoOfCols[i]+"</label>"); } /**  *Loop for the Table data  */ // Loop for the no Of rows for(var j=0;j<data.length;j++){ var row2=tab.insertRow(j+1); row.setAttribute("id",(j+1)+"_row"); // Lopp for the No of Cols in a row for(var k=0;k<NoOfCols.length;k++){ var cell2=row2.insertCell(k); cell2.setAttribute("id",(j+1)+"_row"+k+"_cell"); cell2.innerHTML=data[j][NoOfCols[k]];   } } /**  event Listener for the check Boxes  */ $("input").click(function(event){ var id = event.target.id; if($("#"+id).prop("checked")){ DisplayTable(id); }else{ HideTable(id); } }); /**  * [HideTable Hide the col of a table]

     */ function HideTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","none"); } } /**  * [DisplayTable Display the col of a table]

     */ function DisplayTable(id){ var ColId =id; for(var t=0;t<=data.length;t++){ $("#"+t+"_row"+ColId+"_cell").css("display","table-cell"); } }
试试这个:

<label>
    <input type="checkbox" />
    <span>*** Need text here ***</span>
</label>

将所有内容包装在标签元素中,并在其下方加上一个span,这是我能够显示复选框的唯一方法。

似乎标签的HTML也需要在输入的HTML之后立即显示。如果不需要标签怎么办?该复选框不显示:@Kokodoko保留标签为空?如果我使用单个空间作为标签,则该选项有效-但它仍为标签保留空间,这使得很难获得正确的布局。如果我在CSS中将标签显示设置为“无”,那么整个复选框将不再起作用。顺便说一下,我找到了原因:标签呈现了materialize复选框,materialize隐藏了真正的输入元素!这使得定制变得很困难: