Jinja How to get if checkbox is checked

Viewed 649

I added checkboxes to an HTML table, and I would like to know if have any checkboxes checked. I have a script that works in Jquery that tells me the amount of checkboxes marked in a variable, but I haven't found how to use a Jquery variable in a Jinja if statement condition, or if there's any other way.

So what I would like to get is:

if any checkbox is checked or if Jquery var > 0 (I don't know which way is possible)

{% if current_user.is_authenticated %}
    {% if var > 0 %}
        <form method="POST">
            <div class="form-group">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </form>
    {% else %}
        <div class="form-group">
            <button type="submit" class="btn btn-primary" onclick="submiton()">Submit</button>
        </div>
    {% endif %}
{% else %}
    <div class="form-group">
        <button type="submit" class="btn btn-primary" onclick="submitoff()">Submit</button>
    </div>
{% endif %}

JQuery function to get number of checkbox checked:

<script>
    function qtd_checkbox() {
        var numberOfChecked = $('input:checkbox:checked').length;
    }    
</script>
1 Answers

I was able to find the solution I needed through Python instead of Jinja, so I had to check if I have any checkboxes checked using the request method.

So my solution was: HTML:

...
{% for game in games.items %}
<tr>                
    <td name="ID_GAME" id="ID_GAME">{{ game.ID_GAME}}</td>
    <input type="hidden" name="ID_GAME" value="{{ game.ID_GAME }}" />
    <td name="NM_GAME"> {{ game.NM_GAME}} </td>
    <input type="hidden" name="NM_GAME" value="{{ game.NM_GAME }}" />
    <td name="NR_CRITICSCORE">{{ game.NR_CRITICSCORE}}</td>
    <td>
        <input type="checkbox" id="one_checkbox" name="one_checkbox">
    </td>
</tr>
{% endfor %}

Python:

...
if request.form.getlist('one_checkbox'):
    # Your code here
    print("success!")
Related