【发布时间】:2019-06-11 21:26:06
【问题描述】:
在我的 Flask 应用程序中,我有一个简单形式的 POST 类型和 4 个同名的单选按钮选项。当我选择一个选项并按“下一步”(提交类型的输入)时,会加载一个新页面,其中包含不同的内容但类型相同的表单。
问题是,如果我刷新新加载的页面,提交的表单会使用与之前选择的选项具有相同值的选项。
表格:
<form id="myForm" name="myForm" method="post" >
<table align="center" cellpadding="10" border="0" style="background-color: antiquewhite;">
<tr>
<td rowspan="2"><h3>Is this an <i>origin</i> for the <i>claim?</i></h3></td>
<td><input type="radio" name="op" id="op1" value="1" >Yes</td>
<td><input type="radio" name="op" id="op2" value="2" >No</td>
<td rowspan="2"><input type="submit" value=Next></td>
</tr>
<tr>
<td><input type="radio" name="op" id="op3" value="3" >Invalid Input</td>
<td><input type="radio" name="op" id="op4" value="4" >Don't Know</td>
</tr>
</table>
</form>
处理表单的 Python 代码片段:
if request.method == 'POST':
op = request.form.get('op')
if op:
if op in ['1', '2', '3', '4']:
save_annotation(session.get('claim'),session.get('origin'), op, name)
c_url, o_url = get_least_annotated_page(name, session['claim'])
else:
c_url = session['claim']
o_url = session['origin']
else:
print("NOT POST AND LOGGED IN")
c_url, o_url = get_least_annotated_page(name)
.
.
.
.
.
return render_template('index.html',t1=c_url, t2=o_url)
我只是希望能够在不发布表单的情况下刷新页面。
我已经尝试过使用
document.getElementById("myForm").reset();
还有
<body onload="document.myForm.reset();">
和
autocomplete="off"
此处提供完整代码(annotator.py、app/templates/index.html 和 app/templates/base.html):
https://github.com/MohamedMoustafaNUIG/AnnotatorVM.git
EDIT:session 只是一个我用来存储东西的全局变量。 name 在 python 代码开头初始化,save_annotation() 和 get_least_annotated_page() 是函数。
EDIT2:加载新页面时,所有按钮均未选中。然而,当我刷新时,提交了一个选项。我只是通过查看命令行输出才注意到的。
【问题讨论】:
标签: javascript python html flask