gpt4 book ai didi

javascript - 将数据输入文本框,保存到html表格

转载 作者:行者123 更新时间:2023-11-30 20:46:14 24 4
gpt4 key购买 nike

This is design pagee

这里我想在点击提交按钮后将数据输入到文本框中,将文本框数据插入到html表格中。在表格记录中必须有编辑按钮,点击编辑按钮后,特定记录进入文本框并更新到 html 表格。在这里我使用了 inserCell()(new cell) 之后我无法做。请重播

function addrow()
{
var name = document.getElementById("txtbox1").value;
var password = document.getElementById("txtbox2").value;
var id = document.getElementById("txtbox3").value;
var age = document.getElementById("txtbox4").value;
var table = document.getElementById("mytable");
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);
cell1.innerHTML = name;
cell2.innerHTML = password;
cell3.innerHTML = id;
cell4.innerHTML = age;
cell5.innerHTML = "new cell";
}


//html design page
<head>

<script type="text/javascript" src="JavaScript.js"></script>
</head>
<body>

<table class="auto-style1">
<tr>
<td>NAME</td>
<td><input type="text" value="" id="txtbox1" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>PASSWORD</td>
<td><input type="text" value="" id="txtbox2" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>ID</td>
<td><input type="text" value="" id="txtbox3" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>AGE</td>
<td><input type="text" value="" id="txtbox4" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td></td>
<td><input type="button" VALUE="SUBMIT" id="BTNSUBMIT" onclick="addrow()"/></td>
</tr>
</table>
<table id="mytable" border="1" >
<tr>
<th>NAME</th>
<th>PASSWORD</th>
<th>ID</th>
<th>AGE</th>

</tr>

</table>

</body>
</html>

最佳答案

从 javascript 管理 html 的最简单方法是使用数据绑定(bind)库,例如 knockout js。

这是您想要实现的工作示例:

https://jsfiddle.net/up14yz68/

打开该链接可查看已实现功能和所有功能的现场演示。

代码,html:

<table class="auto-style1">
<tbody data-bind="foreach: fields">
<tr>
<td data-bind="text: title"></td>
<td><input type="text" data-bind="value: value"></td>
</tr>
</tbody>
<tr>
<td></td>
<td><input type="button" VALUE="SUBMIT" data-bind="click: addRow"></td>
</tr>
</table>
<table id="mytable" border="1" >
<tr data-bind="foreach: fields">
<th data-bind="text: name"></th>
</tr>
<tbody data-bind="foreach: rows">
<tr data-bind="foreach: $data">
<td data-bind="text: value"></td>
</tr>
</tbody>
</table>

代码,js:

function viewModel() {
var self = this;

self.rows = ko.observableArray([
[
{title: "name", value: "Santiago"},
{title: "password", value: "Pa55w0rd"},
{title: "id", value: "100255"},
{title: "age", value: "28"}
], [
{title: "name", value: "Stack"},
{title: "password", value: "overflow"},
{title: "id", value: "108888"},
{title: "age", value: "13"}
]
]);

self.fields = [
{title: "name", value: ko.observable("John")},
{title: "password", value: ko.observable("secret123")},
{title: "id", value: ko.observable("10001")},
{title: "age", value: ko.observable("50")},
];

self.addRow = function() {
var newRow = [];
self.fields.forEach(function(field) {
newRow.push({title: field.title, value: field.value()});
field.value("");
});
self.rows.push(newRow);
}
}

ko.applyBindings(new viewModel());

随意浏览 fiddle 以了解发生了什么。您可以在他们的网站上学习使用 knockout,他们有很好的教程。

还有其他数据绑定(bind)的替代方案,比如 Angular,但我觉得 Knockout 非常精简,并且与应用程序的其余部分不显眼。

关于javascript - 将数据输入文本框,保存到html表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com