带有单选按钮的烧瓶手柄形式烧瓶、手柄、单选、按钮

2023-09-06 19:01:14 作者:不羁

我的 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.