Python 如何获取是否在烧瓶上选中复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20941539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get if checkbox is checked on flask
提问by Alpan Karaca
I'm using Bootstrap with Flask Python.
我在 Flask Python 中使用 Bootstrap。
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?
选中复选框时,父标签具有“活动”类,如果选中复选框,我想获取。有什么办法或方法吗?
回答by TwilightSun
I'm not familiar with Flask, but I do know how HTTP works.
我不熟悉 Flask,但我知道 HTTP 是如何工作的。
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.
如果您想知道它是否在服务器端检查,只需检查该表单字段是否存在,如果request.form.get("name")给您 NULL 或异常,则应取消选中该复选框。
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).
如果您想在客户端使用 javascript 了解它,您可以使用 jQuery(因为 jQuery 是 Bootstrap 的基本组件)作为$('xxxx').is(':checked')(用有效的选择器替换 xxxx) .
回答by anurag619
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.
您还可以获取具有相同名称属性的多个复选框的值。

