2 回答
TA贡献1853条经验 获得超6个赞
我在这里看不到 node.js
你可以简化这个
这是获取/发布到 mySql 的方法。忽略 JS 的反应。抓取是一样的
如何在 React.js 中使用 Fetch API 将数据发布到数据库
let table, names;
window.addEventListener("load", () => {
document.getElementById("add").addEventListener("click", addRow)
document.getElementById("save").addEventListener("click", save)
table = document.getElementById("cargoTable");
names = [...document.getElementById("thead").querySelectorAll("th")].map(th => th.innerText).slice(1)
table.addEventListener("click", e => {
const tgt = e.target;
if (tgt.classList.contains("del")) tgt.closest("tr").remove();
})
});
const addRow = () => {
table.innerHTML += `<td><label class="del" style="cursor:pointer">-</label></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>
<td><input class="w3-input" type="text"></td>`;
};
const save = () => {
const vals = [...table.querySelectorAll("tr")]
.map(row => [...row.querySelectorAll("input")]
.map((inp, i) => ({ [names[i]]: inp.value})));
console.log(vals); // here you need to ajax to the server;
/* for example
fetch('your post url here', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(vals)
})
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log(err);
*/
};
<table class="w3-table w3-hoverable w3-bordered w3-card">
<thead id="thead">
<tr>
<th id="add" style="cursor:pointer">+</th>
<th>Comodity</th>
<th>Marks</th>
<th>Description</th>
<th>Pkg.No</th>
<th>Pkg.Kg</th>
<th>Total Bags</th>
<th>Total Weigh</th>
<th>Remarks</th>
</tr>
</thead>
<tbody id="cargoTable"></tbody>
</table>
<button type="button" id="save">Save</button>
TA贡献1798条经验 获得超7个赞
亲爱的,感谢您的回答,但是由于我希望根据后端的要求将结果放在一个数组中,因此我认为以下答案对我来说是正确的我已经为每个单元格分配了相同的名称并通过 javascript 调用它们
function addRow() {
var table = document.getElementById("cargoTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
var cell7 = row.insertCell(6);
var cell8 = row.insertCell(7);
var cell9 = row.insertCell(8);
cell1.innerHTML = '<label onclick="deleteRow(this)" style="cursor:pointer">-</label>';
cell2.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell3.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell4.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell5.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell6.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell7.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell8.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
cell9.innerHTML = '<input name="cellCargo" class="w3-input" type="text">';
}
function documentTabelData(){
var cellData1 = document.getElementsByName("cellDocument");
var i = 0;
var data1 = [];
while(i<cellData1.length){
data1.push(cellData1[i++].value);
}
document.getElementById("docTdata").value = data1;
}
添加回答
举报