通过ParseForm()获取表单选项ID

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

Retrieving form option id through ParseForm()

问题

我正在尝试从HTML表单下拉菜单中获取选项值的id。

假设我在HTML文件中有以下代码:

<select name="film" id="films">
<option id="1">Godfather</option>
</select>

以及在我的Go文件中有以下代码:

func filmFunc(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    film_raw := r.Form["film"]
    film := film_raw[0]
    ...
}

这将给我提供选项的文本("Godfather"),但我需要获取选项的id("1")并将其保存为变量。我该如何做到这一点?

英文:

I am trying to get the option value id from HTML form dropdown.

Let's say I have these lines in my HTML file:

&lt;select name=&quot;film&quot; id=&quot;films&quot;&gt;
&lt;option id=&quot;1&quot;&gt;Godfather&lt;/option&gt;
&lt;/select&gt;

And this in my Go file:

func filmFunc(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    film_raw := r.Form[&quot;film&quot;]
    film := film_raw[0]
    ...
}

This will give me the text from the option (&quot;Godfather&quot;), but I need to get the option id (&quot;1&quot;) and save it as a variable. How can I do it?

答案1

得分: 6

这不是HTML <form>的工作方式。当你在<form>中使用<select>时,你必须在<select>中指定name属性 - 你已经做对了。而且你必须为<option>标签指定value属性,而不是id属性。你也可以指定id属性(例如,如果你想通过id引用该标签),但这不是在提交表单时发送的内容。

当表单被提交时,将发送一个"key"="value"对给<select>,其中"key"将是<select>name属性的值,而"value"将是所选<option>value属性的值。

你可以通过其名称使用Request.FormValue()来获取提交的表单字段的值,注意这也会调用Request.ParseForm()(如果需要的话),所以你甚至可以省略那个调用。

看看这个工作示例:

func formHandler(w http.ResponseWriter, r *http.Request) {
    if selectedFilm := r.FormValue("film"); selectedFilm != "" {
        log.Println("Selected film:", r.FormValue("film"))
    }

    w.Write([]byte(html))
}

func main() {
    http.HandleFunc("/", formHandler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

const html = `<html><body>
<form method="POST" action="/">
    <select name="film" id="films">
        <option value="1">The Godfather</option>
        <option value="2">The Godfather: Part II</option>
    </select>
    <input type="submit" value="Submit">
</form>
</body></html>`

当你选择"The Godfather"并提交时,控制台显示:

2015/12/05 21:18:42 Selected film: 1

当你选择"The Godfather: Part II"并提交时,控制台显示:

2015/12/05 21:18:45 Selected film: 2
英文:

That is not how HTML &lt;form&gt; works. When you use a &lt;select&gt; in a &lt;form&gt;, you have to specify the name attribute at the &lt;select&gt; - you did this right. And you have to specify the value attribute for the &lt;option&gt; tags, not the id. You may specify the id attribute too (e.g. if you want to refer to the tag by its id), but that is not what gets sent when the form is submitted.

When the form is submitted, a &quot;key&quot;=&quot;value&quot; pair will be sent for the &lt;select&gt;, where &quot;key&quot; will be the value of the name attribute of &lt;select&gt;, and &quot;value&quot; will be the value of the value attribute of the &lt;option&gt; that is selected.

And you can get the value of a submitted form field by its name using Request.FormValue(), note that this also calls Request.Parseform() if necessary so you can even omit that call.

See this working example:

func formHandler(w http.ResponseWriter, r *http.Request) {
	if selectedFilm := r.FormValue(&quot;film&quot;); selectedFilm != &quot;&quot; {
		log.Println(&quot;Selected film:&quot;, r.FormValue(&quot;film&quot;))
	}

	w.Write([]byte(html))
}

func main() {
	http.HandleFunc(&quot;/&quot;, formHandler)
	log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
}

const html = `&lt;html&gt;&lt;body&gt;
&lt;form method=&quot;POST&quot; action=&quot;/&quot;&gt;
	&lt;select name=&quot;film&quot; id=&quot;films&quot;&gt;
		&lt;option value=&quot;1&quot;&gt;The Godfather&lt;/option&gt;
		&lt;option value=&quot;2&quot;&gt;The Godfather: Part II&lt;/option&gt;
	&lt;/select&gt;
	&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;`

When you select &quot;The Godfather&quot; and submit, the console shows:

2015/12/05 21:18:42 Selected film: 1

When you select &quot;The Godfather: Part II&quot; and submit, the console shows:

2015/12/05 21:18:45 Selected film: 2

huangapple
  • 本文由 发表于 2015年12月6日 02:03:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/34108987.html
匿名

发表评论

匿名网友

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

确定