我使用的是Node.js和玉石打,目前有這個簡單的模板:後並不包括所有的值
extends layout
block content
h1 #{title}
br
form(action="/updateingredient", method="post")
table.table.table-striped.table-bordered
tr
td Name
td Category
td Available
if (typeof ingredients === "undefined")
tr
td
else
each ingredient in ingredients
tr
td #{ingredient.name}
td #{ingredient.category}
td
input(type="checkbox", name="#{ingredient.id}",
value="#{ingredient.available}", checked=ingredient.available)
button.btn(type="submit") Update Ingredients
當提交這個我撞到在upgradeIngredients
預期:
updateIngredient: function (req, res) {
console.log(req.body);
}
我的問題在於。郵局只包括已選中的複選框,而且複選框的值總是爲false
。我想這是因爲那是Post Post之前的價值。
我最喜歡的是獲取表單中的所有複選框值,選中或不選中。這可能嗎?
從updateIngredient
方法的電流輸出給了我當一個複選框被選中以下(目前只是一個項目測試):
{「b56a5f79-b074-4815-e7e0-4b746b2f65d8」:「假'}
並且當未選中:
{}
編輯
望着構建HTML我看到這一個項目:
<tr>
<td>Ost</td>
<td>Mælkeprodukt</td>
<td>
<input
type="checkbox"
name="b56a5f79-b074-4815-e7e0-4b746b2f65d8"
value="false"
checked="false">
</td>
</tr>
我編輯了我的問題 – Cheesebaron
嗯,閱讀更多,這似乎有一些奇怪的標準。例如。 XHTML需要完整的屬性或者根本不需要(相當於'false'),並且沒有支持的用法,如:checked =「false」。所以如果這是問題,你需要有條件地發出checked =「checked」或根本不發送。 – RobertB