How to pass parameters on onChange of html select with flask-wtf

喜夏-厌秋 提交于 2020-03-18 12:23:04

问题


The following flask code creates a select .. option dropdown menu:

model:

class SelectForm(Form):
    country = SelectField('Country', choices=[
        ('us','USA'),('gb','Great Britain'),('ru','Russia')])

flask app:

@app.route('/new')
def new():
    form = SelectForm()
    return render_template('new.html', form = form )

html file:

<form method=post action="/register">
    {{ render_field(form.country) }}
  <p><input type=submit value=Register>
</form>

macro file the defines render_field:

{% macro render_field(field) %}
  <dt>{{ field.label }}
  <dd>{{ field(**kwargs)|safe }}
  {% if field.errors %}
    <ul class=errors>
    {% for error in field.errors %}
      <li>{{ error }}</li>
    {% endfor %}
    </ul>
  {% endif %}
  </dd>
{% endmacro %}

What is the best way to have onchange submit the results automatically without the user having to press the submit button? Is there a way to change the macro or what's the most elegant way?

thanks


回答1:


It can be done by add javascript onchange function as attribute

<form method=post action="/register">
    {{ render_field(form.country(**{"onchange":"this.form.submit()"})) }}
  <input type=submit value=Register>
</form>


来源:https://stackoverflow.com/questions/38003712/how-to-pass-parameters-on-onchange-of-html-select-with-flask-wtf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!