利用JavaScript实现淄博烧烤季点餐页面,点餐按钮事件实现弹出对话框页面,确定后添加点餐记录页面,能够删除点餐记录,点击删除按钮弹出对话框,确定后删除记录,取消后没有删除记录,能够修改点餐记录
时间: 2023-06-27 21:07:12 浏览: 139
JDBC连接数据库实现增删改查操作,饭店点餐系统。刚学Java时写的,Java入门项目,适合初学者学习.zip
好的,这个需求比较详细,需要分别实现弹出对话框、添加记录、删除记录和修改记录这几个功能。下面我将逐步进行代码实现。
首先,我们需要在HTML页面中创建点餐按钮和点餐记录表格:
```html
<!DOCTYPE html>
<html>
<head>
<title>淄博烧烤季点餐页面</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>淄博烧烤季点餐页面</h1>
<button id="order-btn">点餐</button>
<table>
<thead>
<tr>
<th>菜品</th>
<th>数量</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody id="order-list">
</tbody>
</table>
<div id="order-dialog" class="dialog">
<h2>点餐</h2>
<label for="dish-input">菜品:</label>
<input type="text" id="dish-input" placeholder="请输入菜品名称">
<label for="quantity-input">数量:</label>
<input type="number" id="quantity-input" placeholder="请输入数量">
<label for="price-input">价格:</label>
<input type="number" id="price-input" placeholder="请输入价格">
<button id="confirm-btn">确定</button>
<button id="cancel-btn">取消</button>
</div>
<div id="delete-dialog" class="dialog">
<h2>删除记录</h2>
<p>确定要删除该记录吗?</p>
<button id="delete-confirm-btn">确定</button>
<button id="delete-cancel-btn">取消</button>
</div>
<div id="modify-dialog" class="dialog">
<h2>修改记录</h2>
<label for="modify-dish-input">菜品:</label>
<input type="text" id="modify-dish-input" placeholder="请输入菜品名称">
<label for="modify-quantity-input">数量:</label>
<input type="number" id="modify-quantity-input" placeholder="请输入数量">
<label for="modify-price-input">价格:</label>
<input type="number" id="modify-price-input" placeholder="请输入价格">
<button id="modify-confirm-btn">确定</button>
<button id="modify-cancel-btn">取消</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
在CSS文件中,我们可以对弹出对话框进行样式设置,如下所示:
```css
.dialog {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100;
background-color: white;
border: 1px solid black;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
.dialog h2 {
margin-top: 0;
}
.dialog label {
display: inline-block;
width: 60px;
}
.dialog input {
margin-bottom: 10px;
}
.dialog button {
margin-left: 10px;
}
```
接下来,我们开始编写JavaScript代码。首先,我们需要获取页面中的元素:
```javascript
const orderBtn = document.getElementById('order-btn');
const orderList = document.getElementById('order-list');
const orderDialog = document.getElementById('order-dialog');
const dishInput = document.getElementById('dish-input');
const quantityInput = document.getElementById('quantity-input');
const priceInput = document.getElementById('price-input');
const confirmBtn = document.getElementById('confirm-btn');
const cancelBtn = document.getElementById('cancel-btn');
const deleteDialog = document.getElementById('delete-dialog');
const deleteConfirmBtn = document.getElementById('delete-confirm-btn');
const deleteCancelBtn = document.getElementById('delete-cancel-btn');
const modifyDialog = document.getElementById('modify-dialog');
const modifyDishInput = document.getElementById('modify-dish-input');
const modifyQuantityInput = document.getElementById('modify-quantity-input');
const modifyPriceInput = document.getElementById('modify-price-input');
const modifyConfirmBtn = document.getElementById('modify-confirm-btn');
const modifyCancelBtn = document.getElementById('modify-cancel-btn');
```
然后,我们需要为点餐按钮添加点击事件,使其能够弹出点餐对话框:
```javascript
orderBtn.addEventListener('click', () => {
orderDialog.style.display = 'block';
});
```
接着,我们需要为点餐对话框中的确定按钮添加点击事件,使其能够添加点餐记录到表格中:
```javascript
confirmBtn.addEventListener('click', () => {
const dish = dishInput.value.trim();
const quantity = parseInt(quantityInput.value);
const price = parseFloat(priceInput.value);
if (!dish || !quantity || !price) {
alert('请填写完整的点餐信息!');
return;
}
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${dish}</td>
<td>${quantity}</td>
<td>${price.toFixed(2)}</td>
<td>
<button class="delete-btn">删除</button>
<button class="modify-btn">修改</button>
</td>
`;
orderList.appendChild(tr);
dishInput.value = '';
quantityInput.value = '';
priceInput.value = '';
orderDialog.style.display = 'none';
});
```
这里我们使用了 `innerHTML` 属性来创建一个新的表格行,并设置了菜品、数量、价格以及操作按钮。在添加操作按钮时,我们分别设置了删除按钮和修改按钮,并为它们添加类名,方便后续操作。
接下来,我们需要为删除按钮和修改按钮添加点击事件,分别弹出删除对话框和修改对话框:
```javascript
orderList.addEventListener('click', (event) => {
const target = event.target;
if (target.classList.contains('delete-btn')) {
deleteDialog.style.display = 'block';
} else if (target.classList.contains('modify-btn')) {
modifyDialog.style.display = 'block';
}
});
```
在这里,我们使用了事件委托的方式,只为订单列表添加一个点击事件,然后通过判断点击的元素是否包含删除按钮或修改按钮的类名,来决定弹出哪个对话框。
为了能够删除记录,我们需要为删除对话框中的确定按钮添加点击事件:
```javascript
deleteConfirmBtn.addEventListener('click', () => {
const tr = deleteDialog.parentElement;
tr.parentElement.removeChild(tr);
deleteDialog.style.display = 'none';
});
```
在这里,我们首先获取到当前要删除的表格行,然后从它的父元素中将其删除。
为了能够取消删除记录,我们需要为删除对话框中的取消按钮添加点击事件:
```javascript
deleteCancelBtn.addEventListener('click', () => {
deleteDialog.style.display = 'none';
});
```
类似地,我们还需要为修改对话框中的确定按钮和取消按钮添加点击事件:
```javascript
modifyConfirmBtn.addEventListener('click', () => {
const dish = modifyDishInput.value.trim();
const quantity = parseInt(modifyQuantityInput.value);
const price = parseFloat(modifyPriceInput.value);
if (!dish || !quantity || !price) {
alert('请填写完整的点餐信息!');
return;
}
const tr = modifyDialog.parentElement;
tr.children[0].textContent = dish;
tr.children[1].textContent = quantity;
tr.children[2].textContent = price.toFixed(2);
modifyDishInput.value = '';
modifyQuantityInput.value = '';
modifyPriceInput.value = '';
modifyDialog.style.display = 'none';
});
modifyCancelBtn.addEventListener('click', () => {
modifyDialog.style.display = 'none';
});
```
在这里,我们首先获取到当前要修改的表格行,然后将其中的菜品、数量和价格分别修改为修改对话框中输入的值。
至此,我们就完成了点餐页面的JavaScript代码编写。完整代码如下所示:
```javascript
const orderBtn = document.getElementById('order-btn');
const orderList = document.getElementById('order-list');
const orderDialog = document.getElementById('order-dialog');
const dishInput = document.getElementById('dish-input');
const quantityInput = document.getElementById('quantity-input');
const priceInput = document.getElementById('price-input');
const confirmBtn = document.getElementById('confirm-btn');
const cancelBtn = document.getElementById('cancel-btn');
const deleteDialog = document.getElementById('delete-dialog');
const deleteConfirmBtn = document.getElementById('delete-confirm-btn');
const deleteCancelBtn = document.getElementById('delete-cancel-btn');
const modifyDialog = document.getElementById('modify-dialog');
const modifyDishInput = document.getElementById('modify-dish-input');
const modifyQuantityInput = document.getElementById('modify-quantity-input');
const modifyPriceInput = document.getElementById('modify-price-input');
const modifyConfirmBtn = document.getElementById('modify-confirm-btn');
const modifyCancelBtn = document.getElementById('modify-cancel-btn');
orderBtn.addEventListener('click', () => {
orderDialog.style.display = 'block';
});
confirmBtn.addEventListener('click', () => {
const dish = dishInput.value.trim();
const quantity = parseInt(quantityInput.value);
const price = parseFloat(priceInput.value);
if (!dish || !quantity || !price) {
alert('请填写完整的点餐信息!');
return;
}
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${dish}</td>
<td>${quantity}</td>
<td>${price.toFixed(2)}</td>
<td>
<button class="delete-btn">删除</button>
<button class="modify-btn">修改</button>
</td>
`;
orderList.appendChild(tr);
dishInput.value = '';
quantityInput.value = '';
priceInput.value = '';
orderDialog.style.display = 'none';
});
orderList.addEventListener('click', (event) => {
const target = event.target;
if (target.classList.contains('delete-btn')) {
deleteDialog.style.display = 'block';
} else if (target.classList.contains('modify-btn')) {
modifyDialog.style.display = 'block';
}
});
deleteConfirmBtn.addEventListener('click', () => {
const tr = deleteDialog.parentElement;
tr.parentElement.removeChild(tr);
deleteDialog.style.display = 'none';
});
deleteCancelBtn.addEventListener('click', () => {
deleteDialog.style.display = 'none';
});
modifyConfirmBtn.addEventListener('click', () => {
const dish = modifyDishInput.value.trim();
const quantity = parseInt(modifyQuantityInput.value);
const price = parseFloat(modifyPriceInput.value);
if (!dish || !quantity || !price) {
alert('请填写完整的点餐信息!');
return;
}
const tr = modifyDialog.parentElement;
tr.children[0].textContent = dish;
tr.children[1].textContent = quantity;
tr.children[2].textContent = price.toFixed(2);
modifyDishInput.value = '';
modifyQuantityInput.value = '';
modifyPriceInput.value = '';
modifyDialog.style.display = 'none';
});
modifyCancelBtn.addEventListener('click', () => {
modifyDialog.style.display = 'none';
});
```
阅读全文