如何在 WTForms 中将值设置为隐藏的整数字段
How do I set value to hidden integerfield in WTForms
我有一个表格需要输入 1 个 integer
值。此整数值将是与 html
table
.
中的行关联的 ID
template.html
{% block content %}
<div id="formContainer" class="center top">
<p class="lead">Select simulation session </p>
{% if simulationSessions %}
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>#</th>
<th>Label</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{% for simulationSession in simulationSessions %}
<tr>
<td><input id="id-{{ simulationSession.id }}" name="idRadio" type="radio" value="{{ simulationSession.id }}"> </td>
<td>{{ simulationSession.id }}</td>
<td>{{ simulationSession.label }}</td>
<td>{{ simulationSession.date.strftime('%d-%m-%y %H:%M') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-sm btn-success">Analyse</button>
{% else %}
<div class="alert alert-danger" role="alert">
<strong>Oopsadazy!</strong> No simulations are registered in the database for this user. You need to run a simulation session before analyzing it!
</div>
{% endif %}
</div>
{% endblock %}
每个 table 行都有一个单选按钮,所选按钮的值将是我的表单唯一需要的。我决定使用一个隐藏的 IntegerField
而不是 RadioField
。可以执行后者,但随后我必须解决动态创建选项的问题(当我从 view.py
引用表单时无法将列表作为参数传递时,我决定反对这个问题) .
form.py
class SimulationSessionSelectionForm(Form):
simulationSessoinId = IntegerField('simulationSessoinId', validators=[DataRequired()], widget=HiddenInput())
我的问题是如何从选定的单选按钮中获取值,并在提交表单后将其用作隐藏整数字段的数据?
这也许可以在 html? (这不编译)
{% set form.id.data %}
this.value
{% endset %}
视图看起来像这样:
view.py
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
form = SimulationSessionSelectionForm()
if form.validate_on_submit():
redirect('/somewhere')
userkey = models.User.query.filter_by(id = current_user.get_id()).first().userKey
userSimulationSessions = models.SimulationSession.query.filter_by(userKey = userkey).all()
simulationSessions = []
simulationSessionIds = []
for simulation in userSimulationSessions:
simulationSessions.append({'id' : simulation.sessionID, 'label' : simulation.label, 'date' : simulation.date})
simulationSessionIds.append(simulation.sessionID)
return render_template("dashboard.html", simulationSessions = simulationSessions, form = form)
模板中的这一行将隐藏字段(和 csfr 标记)添加到
{{ form.hidden_tag() }}
我想只要单选按钮更改事件触发,我就可以通过一些 jquery
脚本设置隐藏 IntegerField
的值。为此,我使用了以下代码:
<script>
$( document ).ready(function() {
$('input[type=radio][name=idRadio]').change(function() {
$('#simulationSessoinId').val(this.value)
});
});
</script>
我有一个表格需要输入 1 个 integer
值。此整数值将是与 html
table
.
template.html
{% block content %}
<div id="formContainer" class="center top">
<p class="lead">Select simulation session </p>
{% if simulationSessions %}
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>#</th>
<th>Label</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{% for simulationSession in simulationSessions %}
<tr>
<td><input id="id-{{ simulationSession.id }}" name="idRadio" type="radio" value="{{ simulationSession.id }}"> </td>
<td>{{ simulationSession.id }}</td>
<td>{{ simulationSession.label }}</td>
<td>{{ simulationSession.date.strftime('%d-%m-%y %H:%M') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-sm btn-success">Analyse</button>
{% else %}
<div class="alert alert-danger" role="alert">
<strong>Oopsadazy!</strong> No simulations are registered in the database for this user. You need to run a simulation session before analyzing it!
</div>
{% endif %}
</div>
{% endblock %}
每个 table 行都有一个单选按钮,所选按钮的值将是我的表单唯一需要的。我决定使用一个隐藏的 IntegerField
而不是 RadioField
。可以执行后者,但随后我必须解决动态创建选项的问题(当我从 view.py
引用表单时无法将列表作为参数传递时,我决定反对这个问题) .
form.py
class SimulationSessionSelectionForm(Form):
simulationSessoinId = IntegerField('simulationSessoinId', validators=[DataRequired()], widget=HiddenInput())
我的问题是如何从选定的单选按钮中获取值,并在提交表单后将其用作隐藏整数字段的数据?
这也许可以在 html? (这不编译)
{% set form.id.data %}
this.value
{% endset %}
视图看起来像这样:
view.py
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
form = SimulationSessionSelectionForm()
if form.validate_on_submit():
redirect('/somewhere')
userkey = models.User.query.filter_by(id = current_user.get_id()).first().userKey
userSimulationSessions = models.SimulationSession.query.filter_by(userKey = userkey).all()
simulationSessions = []
simulationSessionIds = []
for simulation in userSimulationSessions:
simulationSessions.append({'id' : simulation.sessionID, 'label' : simulation.label, 'date' : simulation.date})
simulationSessionIds.append(simulation.sessionID)
return render_template("dashboard.html", simulationSessions = simulationSessions, form = form)
模板中的这一行将隐藏字段(和 csfr 标记)添加到
{{ form.hidden_tag() }}
我想只要单选按钮更改事件触发,我就可以通过一些 jquery
脚本设置隐藏 IntegerField
的值。为此,我使用了以下代码:
<script>
$( document ).ready(function() {
$('input[type=radio][name=idRadio]').change(function() {
$('#simulationSessoinId').val(this.value)
});
});
</script>