表单处理与数据提交:食谱/食材管理功能实现咨询
入门级实现方案(基于jQuery)
一、基础页面结构与弹窗实现
先搭好页面按钮和弹窗的基础结构,用jQuery控制弹窗的显示/隐藏:
<!-- 页面操作按钮 --> <button id="addRecipeBtn">新增食谱</button> <button id="addIngredientBtn">新增食材</button> <!-- 新增食材弹窗 --> <div id="ingredientModal" style="display:none; border:1px solid #ccc; padding:20px; position:fixed; top:50%; left:50%; transform:translate(-50%,-50%); background:white;"> <h3>新增食材</h3> <input type="text" id="ingredientName" placeholder="食材名称"> <input type="text" id="ingredientUnit" placeholder="单位(如g/个)"> <button id="saveIngredient">保存食材</button> <button id="closeIngredientModal">关闭</button> </div> <!-- 新增食谱弹窗 --> <div id="recipeModal" style="display:none; border:1px solid #ccc; padding:20px; position:fixed; top:50%; left:50%; transform:translate(-50%,-50%); background:white; width:400px;"> <h3>新增食谱</h3> <input type="text" id="recipeName" placeholder="食谱名称"> <textarea id="recipeDesc" placeholder="食谱描述"></textarea> <h4>可用食材</h4> <div id="ingredientList"></div> <!-- 食材列表渲染位置 --> <button id="saveRecipe">保存食谱</button> <button id="closeRecipeModal">关闭</button> </div>
$(document).ready(function() { // 打开/关闭新增食材弹窗 $('#addIngredientBtn').click(() => $('#ingredientModal').show()); $('#closeIngredientModal').click(() => $('#ingredientModal').hide()); // 打开/关闭新增食谱弹窗(打开时自动渲染食材列表) $('#addRecipeBtn').click(function() { $('#recipeModal').show(); renderIngredientList(); }); $('#closeRecipeModal').click(() => $('#recipeModal').hide()); });
二、初始化本地存储
页面加载时检查localStorage,没有数据就存入初始的食材和食谱:
// 你的Ingredient类 class Ingredient { constructor(name, unit) { this.name = name; this.unit = unit; } } // 初始化localStorage数据 function initLocalStorage() { // 初始化食材数据 if (!localStorage.getItem('ingredients')) { const initialIngredients = [ new Ingredient('鸡蛋', '个'), new Ingredient('面粉', 'g'), new Ingredient('牛奶', 'ml') ]; localStorage.setItem('ingredients', JSON.stringify(initialIngredients)); } // 初始化食谱数据 if (!localStorage.getItem('recipes')) { const initialRecipes = [ { name: '煎蛋饼', desc: '简单易做的早餐', ingredients: ['鸡蛋', '面粉', '牛奶'] } ]; localStorage.setItem('recipes', JSON.stringify(initialRecipes)); } } // 页面加载时执行初始化 $(document).ready(function() { initLocalStorage(); });
三、解决你的三个疑问
1. HTML输入字段与localStorage的数据交互
以新增食材为例,获取输入框的值,存入localStorage:
// 保存食材到localStorage $('#saveIngredient').click(function() { const name = $('#ingredientName').val().trim(); const unit = $('#ingredientUnit').val().trim(); if (!name || !unit) { alert('请填写完整信息'); return; } // 取出现有食材数据,添加新食材后存回 let ingredients = JSON.parse(localStorage.getItem('ingredients')); ingredients.push(new Ingredient(name, unit)); localStorage.setItem('ingredients', JSON.stringify(ingredients)); // 清空输入框并关闭弹窗 $('#ingredientName').val(''); $('#ingredientUnit').val(''); $('#ingredientModal').hide(); });
新增食谱的逻辑类似,还可以结合复选框选择食材:
// 保存食谱到localStorage $('#saveRecipe').click(function() { const name = $('#recipeName').val().trim(); const desc = $('#recipeDesc').val().trim(); // 获取选中的食材 const selectedIngredients = []; $('#ingredientList input:checked').each(function() { selectedIngredients.push($(this).next().text().split('(')[0]); }); if (!name || selectedIngredients.length === 0) { alert('请填写食谱名称并选择食材'); return; } // 取出现有食谱数据,添加新食谱后存回 let recipes = JSON.parse(localStorage.getItem('recipes')); recipes.push({ name: name, desc: desc, ingredients: selectedIngredients }); localStorage.setItem('recipes', JSON.stringify(recipes)); // 清空输入框并关闭弹窗 $('#recipeName').val(''); $('#recipeDesc').val(''); $('#recipeModal').hide(); });
2. 将localStorage数据导出为文件
添加一个导出按钮,把数据转成JSON文件下载:
<button id="exportDataBtn">导出数据</button>
// 导出localStorage数据为JSON文件 $('#exportDataBtn').click(function() { const data = { ingredients: JSON.parse(localStorage.getItem('ingredients')), recipes: JSON.parse(localStorage.getItem('recipes')) }; const jsonStr = JSON.stringify(data, null, 2); const blob = new Blob([jsonStr], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'recipe-data.json'; a.click(); URL.revokeObjectURL(url); });
3. 在食谱表单中渲染食材列表
写一个渲染函数,从localStorage取食材数据,生成带复选框的列表插入到指定位置:
// 渲染食材列表到食谱表单 function renderIngredientList() { const ingredients = JSON.parse(localStorage.getItem('ingredients')); let listHtml = ''; ingredients.forEach(ingredient => { listHtml += ` <div style="margin:8px 0;"> <input type="checkbox" id="ing_${ingredient.name}" value="${ingredient.name}"> <label for="ing_${ingredient.name}">${ingredient.name}(${ingredient.unit})</label> </div> `; }); $('#ingredientList').html(listHtml); }
内容的提问来源于stack exchange,提问作者vlaamseFriet
相关产品推荐
相关产品推荐

