12

I'm using Bootstrap with Flask Python.

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

 <label class="btn btn-danger pzt active">
     <input type="checkbox" name="name" value="1" data-id="0"> Check
 </label>

When checkbox is checked, parent label has class "active", I want to get if checked box is checked. Is there any way or methods?

1
  • 2
    What does request.form.get("name") return? Commented Jan 6, 2014 at 1:52

2 Answers 2

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If request.form.getlist('name_attr') returns 'on', it's likely that you haven't set the value attribute.
5

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).

4 Comments

But the checkbox has no attribute "checked" when i click on it. Label has "active" class or not.
@alpan if then, just make it $('xxxx').hasClass('active');
I'm not checking it with javascript, checking it with python flask
@alpan That's server side check. You should get it by isChecked = 'name' in request.form && request.from.get('name') == '1' (the value check is optional)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.