How to get if checkbox is checked on flask

前端 未结 2 1554
情深已故
情深已故 2020-12-09 05:54

I\'m using Bootstrap with Flask Python.

 request.form.get(\"name\")
 #name is the name of the form element(checkbox)

 
相关标签:
2条回答
  • 2020-12-09 06:28

    you can try the following:

    HTML:
    
    <div class="checkbox">
    <label>
        <input type="checkbox" name="check" value="edit"> New entry
    </label>
    </div>
    

    In flask:

     value = request.form.getlist('check') 
    

    This will give you the value of the the checkbox. Here value will be a list.

    value = [u'edit']
    

    You can also get value of multiple checkboxes with same name attribute.

    0 讨论(0)
  • 2020-12-09 06:46

    I'm not familiar with Flask, but I do know how HTTP works.

    If you want to know if its checked on the server side, just check if that form field exists, if request.form.get("name") gives you NULL or exception, then the checkbox should be unchecked.

    If you want to know it on the client side with javascript, you can use jQuery (as jQuery is a base component of Bootstrap) as $('xxxx').is(':checked') (replace xxxx with a valid selector).

    0 讨论(0)
提交回复
热议问题