Golang – 如何获取所有复选框的值?r.FormValue 不起作用吗?

huangapple go评论70阅读模式
英文:

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 :

&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;0&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;1&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;2&quot; /&gt;

If I check all the options and I use r.FormValue(&quot;product_image_id&quot;) 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[&quot;product_image_id&quot;]

答案3

得分: 4

HTML

    &lt;form method=&quot;post&quot; name=&quot;myform&quot; action=&quot;http://localhost:8081/post&quot; &gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Apple&quot; /&gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Orange&quot; /&gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Grape&quot; /&gt;
     &lt;input type=&quot;submit&quot;&gt;
    &lt;/form&gt;

Go

// ProcessCheckboxes will process checkboxes
func ProcessCheckboxes(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Printf(&quot;%+v\n&quot;, r.Form)
	productsSelected := r.Form[&quot;product_image_id&quot;]
	log.Println(contains(productsSelected, &quot;Grape&quot;))
}

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

    &lt;form method=&quot;post&quot; name=&quot;myform&quot; action=&quot;http://localhost:8081/post&quot; &gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Apple&quot; /&gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Orange&quot; /&gt;
     &lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;Grape&quot; /&gt;
     &lt;input type=&quot;submit&quot;&gt;
    &lt;/form&gt;

Go

// ProcessCheckboxes will process checkboxes
func ProcessCheckboxes(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Printf(&quot;%+v\n&quot;, r.Form)
	productsSelected := r.Form[&quot;product_image_id&quot;]
	log.Println(contains(productsSelected, &quot;Grape&quot;))
}

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

&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;0&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;1&quot; checked/&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;2&quot; checked/&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;3&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;4&quot; checked/&gt;

GO

r.Form[&quot;product_image_id&quot;][0] == &quot;1&quot;
r.Form[&quot;product_image_id&quot;][1] == &quot;2&quot;
r.Form[&quot;product_image_id&quot;][2] == &quot;4&quot;

它将正常工作。
我迟到了吗?

英文:

HTML

&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;0&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;1&quot; checked/&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;2&quot; checked/&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;3&quot; /&gt;
&lt;input type=&quot;checkbox&quot; name=&quot;product_image_id&quot; value=&quot;4&quot; checked/&gt;

GO

r.Form[&quot;product_image_id&quot;][0] == &quot;1&quot;
r.Form[&quot;product_image_id&quot;][1] == &quot;2&quot;
r.Form[&quot;product_image_id&quot;][2] == &quot;4&quot;

It will be working.
Am I late?

huangapple
  • 本文由 发表于 2016年1月19日 22:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/34879373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定