使用单个服务器端变量处理多个复选框 [英] Handle Multiple Checkboxes with a Single Serverside Variable
问题描述
我有以下HTML代码:
I have the following HTML code:
<form method="post">
<h5>Sports you play:</h5>
<input type="checkbox" name="sports_played" value="basketball"> basketball<br>
<input type="checkbox" name="sports_played" value="football"> football<br>
<input type="checkbox" name="sports_played" value="baseball"> baseball<br>
<input type="checkbox" name="sports_played" value="soccer"> tennis<br>
<input type="checkbox" name="sports_played" value="mma"> MMA<br>
<input type="checkbox" name="sports_played" value="hockey"> hockey<br>
<br>
<input class="btn" type="submit">
</form>
然后理想情况下,我想拥有以下python服务器端代码:
And then ideally I would like to have the following python serverside code:
class MyHandler(ParentHandler):
def post(self):
sports_played = self.request.get('sports_played')
#sports_played is a list or array of all the selected checkboxes that I can iterate through
我尝试通过创建HTML sports_played名称和数组sports_played []来做到这一点,但这并没有做任何事情,现在它总是返回第一个选定的项目.
I tried doing this by making the HTML sports_played name and array, sports_played[], but that didn't do anything and right now it just always returns the first selected item.
这可能吗?真的,我只是不想为每个复选框做一个self.request.get('HTML_item'),以防我需要更改HTML而又不想更改python.
Is this possible? Really I just don't want to have to do a self.request.get('HTML_item') for each and every checkbox incase I need to alter the HTML I don't want to have to change the python.
谢谢!
推荐答案
答案显示在请求对象的webapp2文档:
self.request.get('sports_played', allow_multiple=True)
或者您可以使用
self.request.POST.getall('sports_played')
这篇关于使用单个服务器端变量处理多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!