带有单选按钮的烧瓶手柄形式 [英] flask handle form with radio buttons

查看:31
本文介绍了带有单选按钮的烧瓶手柄形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 index.html 是这样的

<form name="myForm" action="" method="post" onsubmit="">
<p>
<input type="radio" name="options" id="option1"> Option1 <br>
<input type="radio" name="options" id="option2"> Option2 <br>
<input type="radio" name="options" id="option3"> Option3 <br>
</p>
<p><input type=submit value=Next></p>
</form>

我需要获取选定的按钮.但由于它们都具有相同的名称,我无法通过编写 request.form['option'] 来实现.如果我让他们的名字不同,用户可以进行多项选择.

I need to get the selected button. But since they all have the same name I cannot do it by writing request.form['option']. If I make their names different, users can make multiple selections.

有没有办法通过它的 id 来获取按钮的状态?如果不是,处理此表单的最简单方法是什么?

Isn't there a way to get a button's state by it's id ? If no, what's the simplest way to handle this form ?

推荐答案

您应该将 value 属性添加到您的每个 input 字段:

You should add the value attribute to each of your input fields:

<input type="radio" name="options" id="option1" value="option1"> Option1 </input><br>
<input type="radio" name="options" id="option2" value="option2"> Option2 </input><br>
<input type="radio" name="options" id="option3" value="option3"> Option3 </input><br>

在您的烧瓶路线中,您可以阅读所选选项:

and in your flask route you can read the selected option:

option = request.form['options']

您将获得所选单选按钮的 .

and you'll get the value of the selected radio button.

这篇关于带有单选按钮的烧瓶手柄形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆