写一个前端(.html)网页代码,网页含一个add按钮和三列表格,表头为物料编号、物料种类,操作。第三列为删除,修改两个button按钮。点击add按钮出现弹窗,弹窗内包含物料编号、物料种类输入框和确定按钮。输入点击确定后,所填内容显示到网页表格中。
时间: 2024-03-23 11:39:36 浏览: 106
好的,这里提供一个简单的HTML代码实现您的需求:
```
<!DOCTYPE html>
<html>
<head>
<title>物料管理</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h1>物料管理</h1>
<button onclick="openDialog()">Add</button>
<table>
<thead>
<tr>
<th>物料编号</th>
<th>物料种类</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<div id="dialog" style="display: none;">
<label for="materialCode">物料编号:</label>
<input type="text" id="materialCode"><br><br>
<label for="materialType">物料种类:</label>
<input type="text" id="materialType"><br><br>
<button onclick="addMaterial()">确定</button>
</div>
<script>
var materialList = [];
function addMaterial() {
var materialCode = document.getElementById("materialCode").value;
var materialType = document.getElementById("materialType").value;
var material = {
code: materialCode,
type: materialType
};
materialList.push(material);
updateTable();
closeDialog();
}
function updateTable() {
var tableBody = document.getElementById("tableBody");
tableBody.innerHTML = "";
materialList.forEach(function(material) {
var row = document.createElement("tr");
var codeCell = document.createElement("td");
var typeCell = document.createElement("td");
var actionCell = document.createElement("td");
codeCell.innerText = material.code;
typeCell.innerText = material.type;
var deleteButton = document.createElement("button");
deleteButton.innerText = "删除";
deleteButton.onclick = function() {
var index = materialList.indexOf(material);
if (index > -1) {
materialList.splice(index, 1);
updateTable();
}
};
var editButton = document.createElement("button");
editButton.innerText = "修改";
editButton.onclick = function() {
var newCode = prompt("请输入新的物料编号", material.code);
if (newCode !== null) {
material.code = newCode;
var newType = prompt("请输入新的物料种类", material.type);
if (newType !== null) {
material.type = newType;
updateTable();
}
}
};
actionCell.appendChild(deleteButton);
actionCell.appendChild(editButton);
row.appendChild(codeCell);
row.appendChild(typeCell);
row.appendChild(actionCell);
tableBody.appendChild(row);
});
}
function openDialog() {
var dialog = document.getElementById("dialog");
dialog.style.display = "block";
}
function closeDialog() {
var dialog = document.getElementById("dialog");
dialog.style.display = "none";
document.getElementById("materialCode").value = "";
document.getElementById("materialType").value = "";
}
</script>
</body>
</html>
```
这个代码实现了您的需求,包括添加物料,修改和删除物料的功能。当点击Add按钮时,会出现一个弹窗,您可以输入物料的编号和种类,然后点击确定按钮将物料添加到表格中。同时,每一行物料都有一个删除和修改按钮,点击可以进行对应的操作。
阅读全文