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

1

9

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

Extrusive answered 23/6, 2016 at 23:59 Comment(0)
B
9

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>
Blithe answered 2/7, 2016 at 6:49 Comment(1)
Works with the render_kw setting as follows... render_kw={"onchange":"this.form.submit()"})Biretta

© 2022 - 2024 — McMap. All rights reserved.