如何使用Golang从表单中获取多选值?

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

How get multiselect values from form using Golang?

问题

我在我的表单中有一个多选输入框,我想在处理程序中获取选定的值,但是我无法获取,我该如何获取这些值?

<form action="process" method="post">
    <select id="new_data" name="new_data" class="tag-select chzn-done" multiple="" style="display: none;">
    <option value="1">111mm1</option>
    <option value="2">222mm2</option>
    <option value="3">012nx1</option>
    </select>
</form>

我的处理程序:

func myHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.FormValue("new_data")) // 结果-> []
    fmt.Println(r.Form("new_data")) // 结果-> []
}

从JS控制台序列化的表单数据,选中了选项1和2:

   >$('#myform').serialize() 
   >"new_data=1&new_data=2"
英文:

I have a multiple select input in my form and I'm trying to get the selected values in my handler, but I can't, how can I get those values?

&lt;form action=&quot;process&quot; method=&quot;post&quot;&gt;
    &lt;select id=&quot;new_data&quot; name=&quot;new_data class=&quot;tag-select chzn-done&quot; multiple=&quot;&quot; style=&quot;display: none;&quot;&gt;
    &lt;option value=&quot;1&quot;&gt;111mm1&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;222mm2&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;012nx1&lt;/option&gt;
    &lt;/select&gt;
&lt;/form&gt;

My Handler:

func myHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.FormValue(&quot;new_data&quot;)) // result-&gt; []
    fmt.Println(r.Form(&quot;new_data&quot;)) // result-&gt; []
}

The form serialized data with option 1 and 2 selected from JS console:

   &gt;$(&#39;#myform&#39;).serialize() 
   &gt;&quot;new_data=1&amp;new_data=2&quot;

答案1

得分: 20

你不能/不应该使用Request.FormValue()函数,因为它只返回一个值。应该使用Request.Form["new_data"],它是一个包含所有值的字符串切片。
但是请注意,如果你不调用r.FormValue(),你必须通过显式调用Request.ParseForm()来触发表单解析(并填充Request.Form映射)。

你还有一个HTML语法错误:name属性的值没有关闭,将其改为:

<select id="new_data" name="new_data" class="tag-select chzn-done" multiple="" style="display: none;">

这是一个完整的应用程序,用于测试它是否工作(省略了错误检查!):

package main

import (
	"fmt"
	"net/http"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		// 表单提交
		r.ParseForm() // 如果不调用r.FormValue(),则需要调用此方法
		fmt.Println(r.Form["new_data"])
	}
	w.Write([]byte(html))
}

func main() {
	http.HandleFunc("/", myHandler)
	http.ListenAndServe(":9090", nil)
}

const html = `
<html><body>
<form action="process" method="post">
    <select id="new_data" name="new_data" class="tag-select chzn-done" multiple="" >
    	<option value="1">111mm1</option>
    	<option value="2">222mm2</option>
    	<option value="3">012nx1</option>
    </select>
    <input type="Submit" value="Send" />
</form>
</body></html>
`
英文:

You can't/shouldn't use the Request.FormValue() function because that only returns 1 value. Use Request.Form[&quot;new_data&quot;] which is a slice of strings containing all the values.
But note that if you don't call r.FormValue(), you have to trigger parsing the form (and populating the Request.Form map) by calling Request.ParseForm() explicitly.

You also have an HTML syntax error: the value of the name attribute is not closed, change it to:

&lt;select id=&quot;new_data&quot; name=&quot;new_data&quot; class=&quot;tag-select chzn-done&quot;
    multiple=&quot;&quot; style=&quot;display: none;&quot;&gt;

Here is a complete app to test that it works (error checks ommited!):

package main

import (
	&quot;fmt&quot;
	&quot;net/http&quot;
)

func myHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == &quot;POST&quot; {
		// Form submitted
		r.ParseForm() // Required if you don&#39;t call r.FormValue()
		fmt.Println(r.Form[&quot;new_data&quot;])
	}
	w.Write([]byte(html))
}

func main() {
	http.HandleFunc(&quot;/&quot;, myHandler)
	http.ListenAndServe(&quot;:9090&quot;, nil)
}

const html = `
&lt;html&gt;&lt;body&gt;
&lt;form action=&quot;process&quot; method=&quot;post&quot;&gt;
    &lt;select id=&quot;new_data&quot; name=&quot;new_data&quot; class=&quot;tag-select chzn-done&quot; multiple=&quot;&quot; &gt;
    	&lt;option value=&quot;1&quot;&gt;111mm1&lt;/option&gt;
    	&lt;option value=&quot;2&quot;&gt;222mm2&lt;/option&gt;
    	&lt;option value=&quot;3&quot;&gt;012nx1&lt;/option&gt;
    &lt;/select&gt;
    &lt;input type=&quot;Submit&quot; value=&quot;Send&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;
`

huangapple
  • 本文由 发表于 2015年2月24日 23:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/28699582.html
匿名

发表评论

匿名网友

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

确定