简体   繁体   English

没有提交按钮的单选按钮

[英]radio buttons without a submit button

I found here an example of how to use radio buttons without a submit button. 我在这里找到一个示例,该示例说明了如何使用没有提交按钮的单选按钮。 I failed to convert this example to Flask. 我未能将此示例转换为Flask。

static/vehicle.js : static/vehicle.js

$('#myform input[type=radio]').on('change', function(event) {
    var result = $(this).val();
    $('#result').html(result);
  })

tamplates/example.html : tamplates/example.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="{{url_for('static', filename='vehicle.js')}}"></script>
<form id="myform" action="" method="post">
    {{ form.example }}
    <div id="result"></div>
</form>

test.py : test.py

from flask import Flask, render_template
from wtforms import Form, RadioField

SECRET_KEY = 'development'

app = Flask(__name__)
app.config.from_object(__name__)


class SimpleForm(Form):
    example = RadioField(
        'Label', choices=[('home', 'home'), ('side1', 'side1'), ('side1', 'side1')])


@app.route('/', methods=['post', 'get'])
def hello_world():
    form = SimpleForm()
    if form.validate():
        print(form.example.data)
    else:
        print(form.errors)
    return render_template('example.html', form=form)

if __name__ == '__main__':
    app.run(debug=True)

Unfortunately, pressing the radio buttons seems not to print the value. 不幸的是,按单选按钮似乎无法打印该值。

What did I miss? 我错过了什么?

Thank you in advance 先感谢您

When it comes to JS you are trying to bind handler to change event when document is not ready and automatic form submission is missing. 当涉及到JS时,您试图绑定处理程序以在文档尚未准备好并且缺少自动提交表单时更改事件。

It works when you put your code in $( document ).ready() callback and add $('#myform').submit(); 当您将代码放入$( document ).ready()回调并添加$('#myform').submit(); :

$( document ).ready(function() {

    $('#myform input[type=radio]').on('change', function(event) {
        var result = $(this).val();
        $('#result').html(result);
        $('#myform').submit();
    });
});

However some changes to test.py need to be done in order to make it a working example: 但是,为了使它成为一个有效的示例,需要对test.py进行一些更改:

from flask import Flask, render_template, request
from wtforms import Form, RadioField

SECRET_KEY = 'development'

app = Flask(__name__)
app.config.from_object(__name__)


class SimpleForm(Form):
    example = RadioField(
        'Label', choices=[('home', 'home'), ('side1', 'side1'), ('side1', 'side1')])


@app.route('/', methods=['post', 'get'])
def hello_world():
    form = SimpleForm(request.form)
    if request.method == 'POST':
        if form.validate():
            print(form.example.data)
        else:
            print(form.errors)
    return render_template('example.html', form=form)


if __name__ == '__main__':
    app.run(debug=True)

You didn't pass request data to your form which always resulted in error and form validation should be performed only when it's POST request. 您没有将请求数据传递到表单,这总是会导致错误,并且仅在POST请求时才应执行表单验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM