<h1>用户顾客列表</h1> <div> <p>顾客姓名:</p> <input type="text" id="name" /> <p>顾客职业:</p> <input type="text" id="career" /> <p>电话号码:</p> <input type="text" id="phone" /> <button onclick="saveData()">保存</button> </div> <script> function saveData() { var name = document.getElementById("name").value; var career = document.getElementById("career").value; var phone = document.getElementById("phone").value; localStorage.setItem("name", name); localStorage.setItem("career", career); localStorage.setItem("phone", phone); location.href = "edit.jsp"; } </script>如何在这个代码的基础上增加删除按钮,单击删除按钮,弹出是否删除的确认提示框,单击确定按钮后,删除所选的顾客信息。 删除后列表刷新,显示删除后的列表。单击取消按钮后,不会删除顾客信息。
时间: 2023-10-24 14:08:37 浏览: 36
可以在用户顾客列表的div中添加一个table,用来显示顾客信息。在每个顾客信息后面添加一个删除按钮,单击删除按钮时调用相应的函数,弹出确认提示框。如果用户点击确定,则从localStorage中删除相应的顾客信息,并重新加载用户列表。
以下是修改后的代码:
```
<h1>用户顾客列表</h1>
<div>
<p>顾客姓名:</p>
<input type="text" id="name" />
<p>顾客职业:</p>
<input type="text" id="career" />
<p>电话号码:</p>
<input type="text" id="phone" />
<button onclick="saveData()">保存</button>
</div>
<table id="customerTable">
<tr>
<th>姓名</th>
<th>职业</th>
<th>电话号码</th>
<th>操作</th>
</tr>
<script>
// 加载已有的顾客信息
var customerList = JSON.parse(localStorage.getItem("customerList")) || [];
for (var i = 0; i < customerList.length; i++) {
var customer = customerList[i];
var row = document.createElement("tr");
row.innerHTML = "<td>" + customer.name + "</td><td>" + customer.career + "</td><td>" + customer.phone + "</td><td><button onclick='deleteCustomer(" + i + ")'>删除</button></td>";
document.getElementById("customerTable").appendChild(row);
}
function saveData() {
var name = document.getElementById("name").value;
var career = document.getElementById("career").value;
var phone = document.getElementById("phone").value;
var customer = { "name": name, "career": career, "phone": phone };
customerList.push(customer);
localStorage.setItem("customerList", JSON.stringify(customerList));
location.href = "edit.jsp";
}
function deleteCustomer(index) {
if (confirm("确定要删除该顾客信息吗?")) {
customerList.splice(index, 1);
localStorage.setItem("customerList", JSON.stringify(customerList));
location.reload();
}
}
</script>
</table>
```
在这个代码中,我们首先加载已有的顾客信息,然后根据每个顾客信息创建一个新的行,并在最后一列添加一个删除按钮。当用户单击删除按钮时,我们调用deleteCustomer函数,弹出确认提示框。如果用户单击确定,则从localStorage中删除相应的顾客信息,并重新加载用户列表。