英文:
Golang - How to get all the value of checkbox? r.FormValue is not working?
问题
这是我的HTML代码:
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" />
<input type="checkbox" name="product_image_id" value="2" />
如果我勾选了所有选项,并使用r.FormValue("product_image_id")
来获取勾选选项的值,我只能得到值0
。
我的意思是,我只能获取到第一个值,无法获取其他被勾选的值。
请帮助我,谢谢。
英文:
This is my HTML :
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" />
<input type="checkbox" name="product_image_id" value="2" />
If I check all the options and I use r.FormValue("product_image_id")
to get the value of checked options, I will only get the value 0
.
I mean I can only get the first value, and I can't get other value although it was checked.
Please help me. Thanks.
答案1
得分: 7
Request.FormValue
(https://golang.org/pkg/net/http/#Request.FormValue)只返回第一个值,如果有多个值。根据文档:
> FormValue
返回查询中指定组件的第一个值。
>
> ...
>
> 要访问同一个键的多个值,请调用 ParseForm
,然后直接检查 Request.Form
。
英文:
Request.FormValue
only returns the first value if there is more than one. From the Documentation:
> FormValue returns the first value for the named component of the query.
>
> ...
>
> To access multiple values of the same key, call ParseForm
and then
> inspect Request.Form
directly.
答案2
得分: 5
r.FormValue
返回选项列表中的第一个值,而 r.Form
返回一个列表。你可以通过 r.Form["product_image_id"]
来访问你的值。
英文:
r.FormValue
returns the first value from the list of options
instead use r.Form
, this returns a list.
you can access your values by
r.Form["product_image_id"]
答案3
得分: 4
HTML
<form method="post" name="myform" action="http://localhost:8081/post" >
<input type="checkbox" name="product_image_id" value="Apple" />
<input type="checkbox" name="product_image_id" value="Orange" />
<input type="checkbox" name="product_image_id" value="Grape" />
<input type="submit">
</form>
Go
// ProcessCheckboxes will process checkboxes
func ProcessCheckboxes(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Printf("%+v\n", r.Form)
productsSelected := r.Form["product_image_id"]
log.Println(contains(productsSelected, "Grape"))
}
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set展开收缩 = struct{}{}
}
_, ok := set[item]
return ok
}
Output
Orange和Grape复选框被选中并提交
map[product_image_id:[Orange Grape]]
2016/10/27 16:16:06 true
Apple和Orange复选框被选中并提交
map[product_image_id:[Apple Orange]]
2016/10/27 16:17:21 false
英文:
HTML
<form method="post" name="myform" action="http://localhost:8081/post" >
<input type="checkbox" name="product_image_id" value="Apple" />
<input type="checkbox" name="product_image_id" value="Orange" />
<input type="checkbox" name="product_image_id" value="Grape" />
<input type="submit">
</form>
Go
// ProcessCheckboxes will process checkboxes
func ProcessCheckboxes(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Printf("%+v\n", r.Form)
productsSelected := r.Form["product_image_id"]
log.Println(contains(productsSelected, "Grape"))
}
func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set展开收缩 = struct{}{}
}
_, ok := set[item]
return ok
}
Output
Orange and Grape checboxes are checked and submitted
map[product_image_id:[Orange Grape]]
2016/10/27 16:16:06 true
Apple and Orange checboxes are checked and submitted
map[product_image_id:[Apple Orange]]
2016/10/27 16:17:21 false
答案4
得分: 2
HTML
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" checked/>
<input type="checkbox" name="product_image_id" value="2" checked/>
<input type="checkbox" name="product_image_id" value="3" />
<input type="checkbox" name="product_image_id" value="4" checked/>
GO
r.Form["product_image_id"][0] == "1"
r.Form["product_image_id"][1] == "2"
r.Form["product_image_id"][2] == "4"
它将正常工作。
我迟到了吗?
英文:
HTML
<input type="checkbox" name="product_image_id" value="0" />
<input type="checkbox" name="product_image_id" value="1" checked/>
<input type="checkbox" name="product_image_id" value="2" checked/>
<input type="checkbox" name="product_image_id" value="3" />
<input type="checkbox" name="product_image_id" value="4" checked/>
GO
r.Form["product_image_id"][0] == "1"
r.Form["product_image_id"][1] == "2"
r.Form["product_image_id"][2] == "4"
It will be working.
Am I late?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论