简体   繁体   English

如何在javascript中将输入字段动态添加到我的html表格行内的单元格中?

[英]How do I dynamically add an input field into a cell inside my html table row in javascript?

This is my code:这是我的代码:

var tab=document.getElementById("quarter_details");
var input = document.createElement("input");
input.type = "text";
input.placeholder = "dd/mm/yyyy";
input.id = "vac";

var row = tab.insertRow(5);
var cell = row.insertCell(0);
var cell1 = row.insertCell(1);

cell.innerHTML = "<b>Date:</b>";
cell1.innerHTML = input; //[object HTMLInputElement]

When I run the code, the input text field doesn't appear.当我运行代码时,输​​入文本字段不会出现。 What do I do?我该怎么办?

EDIT: Tried using cell1.appendChild = input .编辑:尝试使用cell1.appendChild = input Now it shows an empty cell, with no input field现在它显示一个空单元格,没有输入字段

使用appendChild方法:

cell1.appendChild(input);

Use insertAdjacentHTML .使用insertAdjacentHTML Here's a minimal reproducable example .这是一个最小的可重现示例

 const createDateTimeInput = (forCell, id) => { forCell.textContent = ``; // empty the cell forCell.insertAdjacentHTML( `beforeend`, `<b>Date</b> <input type="text" placeholder="dd/mm/yyyy" id="${id}">`); }; const firstCellOfSecondRow = document.querySelector(`table tbody tr:nth-child(2) > td`); setTimeout(() => createDateTimeInput(firstCellOfSecondRow, `vac`), 2000);
 thead th { text-align: left }
 <table> <thead> <tr> <th>first column</th> <th>second column</th> </tr> </thead> <tbody> <tr> <td>cell</td> <td>cell</td> </tr> <tr> <td>cell</td> <td>cell</td> </tr> </tbody> </table>

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

相关问题 如何向 javascript 中动态创建的表格行添加工具提示? - How do I add a tooltip to a dynamically created table row in javascript? 如何将范围滑块输入字段值绑定到html表中的正确输入字段? - How do I bind my range slider input field value to the correct input field in my html table? 如何在表字段中添加带有输入文本的表行 - How to add table row with input text inside the table field 在Javascript的HTML表格中动态添加表格行 - dynamically add table row in HTML Table in Javascript 为什么在动态添加包含div的行时,我的HTML表列会重新调整大小? - Why do my HTML table columns resize when I dynamically add a row containing a div? 如何使用JavaScript在HTML输入字段上动态填充JSON数据? - How Do I dynamically populate JSON data on html input field using javascript? 如何将动态创建的输入(或文本框)值发送到iframe,并在iframe中将其显示为HTML? - How do I send my dynamically created input (or textbox) values to an iframe and show it as HTML inside the iframe? javascript:如何将div动态添加到表格单元格? - javascript: How to add div to table cell dynamically? 如何获取表单元格中的type输入值? - How do I get the value of type input inside a table cell? 将 html 表格单元格从 javascript 制作成 html 输入字段 - Making a html table cell into a html input field from javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM