使用单个服务器端变量处理多个复选框

编程入门 行业动态 更新时间:2024-10-26 00:25:31
本文介绍了使用单个服务器端变量处理多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下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')

更多推荐

使用单个服务器端变量处理多个复选框

本文发布于:2023-10-12 23:15:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486158.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   服务器端   变量   复选框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!