Flask 在页面重新刷新后保留复选框

发布于 2025-01-18 05:45:19 字数 309 浏览 2 评论 0原文

我使用了我尝试使用Java脚本的烧瓶模板的代码

<div class="rows">
     <h3>In oven</h3>
     {% for checkbox in checkboxes: %}
     <li><input type="checkbox"></li>
     {% endfor %}
</div>     

,但是在刷新复选框后,即使我以前检查过它也是如此。我想检查一下,然后将页面刷新后,以保持支票。有什么想法如何做?

I have this piece of code of a flask template

<div class="rows">
     <h3>In oven</h3>
     {% for checkbox in checkboxes: %}
     <li><input type="checkbox"></li>
     {% endfor %}
</div>     

I tried using java script but after refresh the checkboxes resets even if I checked it before. I want to checked it and after refreshing the page the check to stay. Any ideas how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2025-01-25 05:45:19

您需要将复选框的值存储在某处,以便在再次加载页面时对其进行设置。

  • 本地存储
  • Flask 会话
  • 数据库
  • Ajax 查询
  • 使用表单

该值必须是可以评估为 true/false 的值。

检索值后,您需要评估是否在输入中添加选中的属性。

在此示例中,我在视图上使用变量。

#app.py
@app.route("/")
def sample():
    your_variable=True
    return render_template('index.html', your_variable=your_variable)

#index.html
<div class="rows">
     <h3>In oven</h3>
     {% for checkbox in checkboxes: %}
     <li><input type="checkbox" {{'checked="checked"' if your_variable else ""}}></li>
     {% endfor %}
</div>     

当然,对于更完整的问题,这可以更具体

You need to store somewhere the value of the checkbox to set it when loading the page again.

  • Local storage
  • Flask session
  • Database
  • Ajax query
  • With a form

This value has to be something that can be evaluated as true/false.

After retrieving the value you need to evaluate for add or not the checked property in the input.

In this example, I'm using a variable on the view.

#app.py
@app.route("/")
def sample():
    your_variable=True
    return render_template('index.html', your_variable=your_variable)

#index.html
<div class="rows">
     <h3>In oven</h3>
     {% for checkbox in checkboxes: %}
     <li><input type="checkbox" {{'checked="checked"' if your_variable else ""}}></li>
     {% endfor %}
</div>     

Of course, with a more complete question this can be more specific

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文