我有一个表,填充者是
<button type="submit" name="update" value = company.index style = margin-left:45%>Update</button>
....
{% for product in products%}
<tr {{product .index}}>
<th scope="row">{{ product .productname }}</th>
<td> {{ product .description }} </td>
<td><form method = "POST"> <input type="checkbox" name="checkbox{{product .index}}" value = {{product .index}}{% if product .like == 1 %} checked {% else %} {% endif %}></form>{{product .like}}</td>
{% endfor %}
根据列表products
中的产品数量来填充表格。然后,我在app.py
中尝试查看是否选中了任何框(它们都具有不同的名称),问题是,如果我有多个被选中的行,则只有第一行是返回值的行
app.py
@app.route('/', methods = ['GET', 'POST'])
def index():
....
for key in request.form:
print(key)
return render_template('index.html', products = products)
因此,如果我的清单products
包含3种产品,则它们将分别位于表中的一行上,并且所有产品旁边都有一个框。但是,当我选中所有复选框并按“更新”按钮时,将仅打印第一个按钮的名称。如何访问表格中所有按钮的值?
答案 0 :(得分:1)
你又哈哈
问题是每个产品都有一个表单元素,因此,如果要检查每个项目,都需要用表单元素包装所有这些项目。
另一个选择是使用我在另一个问题中提到的javascript。那么您可以删除所有表单元素,而改为使用javascript函数。
示例:
<form method="POST">
{% for product in products%}
<tr {{product .index}}>
<th scope="row">{{ product .productname }}</th>
<td> {{ product .description }} </td>
<td>
<input type="checkbox" name="checkbox{{product .index}}"
value={{product .index}}{% if product .like == 1 %} checked {% else %} {% endif %}>
{{product .like}}
</td>
{% endfor %}
<button type="submit" name="update" value="company.index" style="margin-left:45%">Update</button>
</form>