基于Python变量的HTML默认值fo广播按钮输入类型

发布于 2025-01-27 19:29:17 字数 1002 浏览 4 评论 0原文

因此,即时通讯在使用用户上传电子表格,然后可以在HTML页面上使用该电子表格的参数进行播放。一个参数是一个只能是1、2或3的值。用户能够通过选择带有这些选项的单选按钮来更改此值。

这一切都很好,但是我希望广播按钮的默认值是用户上传的初始电子表格中的任何内容。

我的想法是做类似的事情:

main.py

@app.route('/', methods=['GET','POST'])
def params_form():

    value = 2
    value_dict = {'1': 1==value, '2': 2==value, '3': 3==value}
    
    return render_template('home/sheet.html', value_dict)

Sheet.html

<div>
    <input type="radio" id="n1" name="nyears" value=1 checked={{value_dict["1"]}}>
        <label for="n1"> 1 </label>

    <input type="radio" id="n2" name="nyears" value=2 checked={{value_dict["2"]}}>
        <label for="n2"> 2 </label>

    <input type="radio" id="n3" name="nyears" value=3 checked={{value_dict["3"]}}>
        <label for="n3"> 3 </label>
</div>

,但不幸的是,对“检查”属性的任何使用都会检查框(在这种情况下,这是最后使用检查属性的属性)。似乎拥有默认检查值的唯一方法是仅在一个输入行上使用检查属性。

任何帮助使此工作的工作将不胜感激 提前致谢!

so im making a Flask app where a user uploads a spreadsheet and then can play with the parameters of that spreadsheet on a html page. One parameter is a value which can only be 1, 2 or 3. The user is able to change this value by selecting a radio button with these options.

This all works fine but i would like the default value of the radio button to be whatever was in the initial spreadsheet that the user uploaded.

my idea was to do something like this:

main.py

@app.route('/', methods=['GET','POST'])
def params_form():

    value = 2
    value_dict = {'1': 1==value, '2': 2==value, '3': 3==value}
    
    return render_template('home/sheet.html', value_dict)

sheet.html

<div>
    <input type="radio" id="n1" name="nyears" value=1 checked={{value_dict["1"]}}>
        <label for="n1"> 1 </label>

    <input type="radio" id="n2" name="nyears" value=2 checked={{value_dict["2"]}}>
        <label for="n2"> 2 </label>

    <input type="radio" id="n3" name="nyears" value=3 checked={{value_dict["3"]}}>
        <label for="n3"> 3 </label>
</div>

But unfortunately, any use of the 'checked' attribute will check the box (in this case number 3, as it is the last to use the checked attribute). It seems that the only way to have a default checked value is to only use the checked attribute on one input line.

Any help getting this to work will be greatly appreciated
Thanks in advance!

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

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

发布评论

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

评论(1

坚持沉默 2025-02-03 19:29:17

您需要在render_template中以关键字的方式设置变量名:

@app.route('/', methods=['GET','POST'])
def params_form():

    value = 2
    value_dict = {'1': 1==value, '2': 2==value, '3': 3==value}
    
    return render_template('home/sheet.html', value_dict=value_dict)
<div>
    <input type="radio" id="n1" name="nyears" value=1  {{ 'checked' if value_dict["1"] else '' }} >
        <label for="n1"> 1 </label>

    <input type="radio" id="n2" name="nyears" value=2  {{ 'checked' if value_dict["2"] else '' }} >
        <label for="n2"> 2 </label>

    <input type="radio" id="n3" name="nyears" value=3  {{ 'checked' if value_dict["3"] else '' }} >
        <label for="n3"> 3 </label>
</div>

You need to set the variable name in render_template by passing it as keyword:

@app.route('/', methods=['GET','POST'])
def params_form():

    value = 2
    value_dict = {'1': 1==value, '2': 2==value, '3': 3==value}
    
    return render_template('home/sheet.html', value_dict=value_dict)
<div>
    <input type="radio" id="n1" name="nyears" value=1  {{ 'checked' if value_dict["1"] else '' }} >
        <label for="n1"> 1 </label>

    <input type="radio" id="n2" name="nyears" value=2  {{ 'checked' if value_dict["2"] else '' }} >
        <label for="n2"> 2 </label>

    <input type="radio" id="n3" name="nyears" value=3  {{ 'checked' if value_dict["3"] else '' }} >
        <label for="n3"> 3 </label>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文