使用 select 输入时无法从表单接收值
Unable to recieve value from form when using select input
我有一个简单的表单,允许用户提交两种类型的 ID:一种是他们自己输入的,另一种是应该根据他们在表单上所做的选择提交的值输入。
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
heroes = requests.get(f"{BASE_URL}/heroes").json()
return render_template("index.html", heroes=heroes)
else:
steam32_id = request.form.get('steam32_id')
hero_id = request.form.get('hero_id')
return {"steam32_id": steam32_id, "hero_id": hero_id}
<form method="post" action="{{ url_for('index') }}">
<label for="steam32_id">Steam ID</label>
<input type="text" id="steam32_id" name="steam32_id"> <br>
<label for="hero_list">Select Hero</label>
<select id="hero_list" name="hero_list">
{% for hero in heroes %}
<option value={{ hero.id }}>{{ hero.localized_name }}</option>
{% endfor %}
</select> <br>
<input type="submit" value="Submit">
</form>
当尝试接收 hero[id]
时,它当前输出 null
{
"hero_id": null,
"steam32_id": ""
}
不管用户选择什么英雄。
我显然误会了什么;非常感谢任何指导。
作为参考,我正在使用OpenDota's API。
我意识到我试图访问 select
字段中的错误字段,而且我的代码中甚至不存在这样的字段。
我意识到 select
输入的名称是我需要的,而不是 option
.
中的 value
<select id="hero_list" name="hero_id">
{% for hero in heroes %}
<option value={{ hero.id }} >{{ hero.localized_name }}</option>
{% endfor %}
</select>
我只是重命名了 select
输入中的 name
字段。
我有一个简单的表单,允许用户提交两种类型的 ID:一种是他们自己输入的,另一种是应该根据他们在表单上所做的选择提交的值输入。
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
heroes = requests.get(f"{BASE_URL}/heroes").json()
return render_template("index.html", heroes=heroes)
else:
steam32_id = request.form.get('steam32_id')
hero_id = request.form.get('hero_id')
return {"steam32_id": steam32_id, "hero_id": hero_id}
<form method="post" action="{{ url_for('index') }}">
<label for="steam32_id">Steam ID</label>
<input type="text" id="steam32_id" name="steam32_id"> <br>
<label for="hero_list">Select Hero</label>
<select id="hero_list" name="hero_list">
{% for hero in heroes %}
<option value={{ hero.id }}>{{ hero.localized_name }}</option>
{% endfor %}
</select> <br>
<input type="submit" value="Submit">
</form>
当尝试接收 hero[id]
时,它当前输出 null
{
"hero_id": null,
"steam32_id": ""
}
不管用户选择什么英雄。
我显然误会了什么;非常感谢任何指导。
作为参考,我正在使用OpenDota's API。
我意识到我试图访问 select
字段中的错误字段,而且我的代码中甚至不存在这样的字段。
我意识到 select
输入的名称是我需要的,而不是 option
.
value
<select id="hero_list" name="hero_id">
{% for hero in heroes %}
<option value={{ hero.id }} >{{ hero.localized_name }}</option>
{% endfor %}
</select>
我只是重命名了 select
输入中的 name
字段。