JavaScript,创建新的 html 元素 : the content of the new created <td> is inserted in old <td>

标签 javascript html

我正在创建一个包含 JavaScript 代码的网页,我想做的是,每当我单击按钮时,代码都会创建一个新的 <tr>并在每个 <tr>会有很多<td>元素。 但问题是,每当我单击按钮时,都会创建元素,但每个 <td> 的内容插入到第一行 <tr> 的单元格中。 这意味着全新 <td> s 为空。

举个例子:如果我点击“Ajouter une ligne”按钮两次,看起来会创建 2 行: enter image description here

但是如果我们看一下html代码,我们可以看到第二个<tr>是空的: enter image description here

这是代码.js

<script>
///////////////
var click=0;
function addform() {
click +=1;
var element = document.getElementById("div1");
//create a tr
var id_tr="tr"+click;
create_tr(element, id_tr);

//create the content of td
 


var designation = document.createElement("INPUT");
textinput(designation, "text" , "designation", "form-control", 
"Désignation",click);

var datef = document.createElement("INPUT");
textinput(datef, "date" , "datefacture", "form-control", "Date de 
Facture",click);

var datep = document.createElement("INPUT");
textinput(datep, "date" , "datepaie", "form-control", "Date de Paie",click);

var mht = document.createElement("INPUT");
textinput(mht, "text" , "mht", "form-control", "Montant HT",click);

var mtva = document.createElement("INPUT");
textinput(mtva, "text" , "mtva", "form-control", "Montant TVA",click);

var mttc = document.createElement("INPUT");
textinput(mttc, "text" , "mttc", "form-control", "Montant TTC",click);

var file = document.createElement("INPUT");
textinput(file, "text" , "file", "form-control", "Choose File",click);

var importer = document.createElement("INPUT");
textinput(importer, "button" , "importer", "btn btn-primary", "Choose 
File",click);


var elmtr=document.getElementById(id_tr);
create_tds(elmtr, designation, 1 );
create_tds(elmtr, datef, 2 );
create_tds(elmtr, datep, 3);
create_tds(elmtr, mht, 4);
create_tds(elmtr, mtva, 5);
create_tds(elmtr, mttc, 6);
create_tds(elmtr, file, 7);
create_tds(elmtr, importer, 8);
}
function create_tr(element, id_tr){
var trr = document.createElement("TR");
trr.setAttribute("id", id_tr);
element.appendChild(trr);

}
function create_tds(elmtr,input_td, clicktd){
var y = document.createElement("TD");
var td="td";
y.setAttribute("id", td+clicktd);
elmtr.appendChild(y);
var elmtd=document.getElementById(td+clicktd);
elmtd.appendChild(input_td);

}
function textinput(x, type, name, classe, placeholder,click){

x.setAttribute("type", type);
x.setAttribute("name", name+click);
x.setAttribute("class", classe);
x.setAttribute("placeholder", placeholder);
if (name=="file") {

    x.setAttribute("disabled", "disabled");
}
if (type == "button") {
x.setAttribute("value", "importer");    
}
}
</script>

最佳答案

根据你的截图,你犯了一个基本错误:id是一个清楚地识别页面中每个元素的工具。意味着 id 必须是唯一的。表格单元格的 ID 多次出现。

如果您有机会使用 Jquery,您可以将 ids 更改为 -dummy- css 类。多次使用 css 类的名称是有效的 html 代码。 :-)

关于JavaScript,创建新的 html 元素 : the content of the new created <td> is inserted in old <td>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49191359/

相关文章:

javascript - XMLHttpRequest.open,它在远程网站上有效吗?

javascript - threejs 中的环境遮挡

html - 更改特定元素的滚动速度

javascript - API Foursquare javascript 不起作用

javascript - Redux 表单验证在 onChange 处理函数之后运行

javascript - 选择具有特定类别的 sibling

css - 如何使用 Big Cartel 隐藏菜单

html - 翻转 Bootstrap 缩略图时 div 的绝对定位

php - Datalist HTML 中的分界线不起作用

html - 固定 block 元素