1 回答
TA贡献1818条经验 获得超3个赞
newPlan.monday您需要使用 an循环遍历 的条目index,以便可以将 current 的值设置key为适当的元素值。在本例中为monday-index(monday-1)。
这是一个例子:
const newPlan = {
name: '',
description: '',
number: '',
monday: {
breakfast: '',
secondBreakfast: '',
soup: '',
secondMeal: '',
supper: '',
}
};
let index = 1;
newPlan.name = document.getElementById('planName').innerHTML;
newPlan.description = document.getElementById('planDescription').innerHTML;
newPlan.number = document.getElementById('planWeekNumber').innerHTML;
for (let [key, value] of Object.entries(newPlan.monday)) {
newPlan.monday[key] = document.getElementById(`monday-${index}`).value;
index++;
}
console.log(newPlan);
<tr>
<td>PONIEDZIAŁEK</td>
<div id="planName">Plan 1</div>
<div id="planDescription">Tasty food.</div>
<div id="planWeekNumber">1</div>
<td>
<input list="monday-breakfast" id="monday-1" value="Ham and eggs">
<datalist id="monday-breakfast"></datalist>
</td>
<td>
<input list="monday-second-breakfast" id="monday-2" value="Cereal">
<datalist id="monday-second-breakfast"></datalist>
</td>
<td>
<input list="monday-soup" id="monday-3" value="Meat soup">
<datalist id="monday-soup"></datalist>
</td>
<td>
<input list="monday-second-meal" id="monday-4" value="Hamburger">
<datalist id="monday-second-meal"></datalist>
</td>
<td>
<input list="monday-supper" id="monday-5" value="Toast">
<datalist id="monday-supper"></datalist>
</td>
</tr>
- 1 回答
- 0 关注
- 71 浏览
添加回答
举报