英文:
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:
<select name="film" id="films">
<option id="1">Godfather</option>
</select>
And this in my Go file:
func filmFunc(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
film_raw := r.Form["film"]
film := film_raw[0]
...
}
This will give me the text from the option ("Godfather"
), but I need to get the option id ("1"
) 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 <form>
works. When you use a <select>
in a <form>
, you have to specify the name
attribute at the <select>
- you did this right. And you have to specify the value
attribute for the <option>
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 "key"="value"
pair will be sent for the <select>
, where "key"
will be the value of the name
attribute of <select>
, and "value"
will be the value of the value
attribute of the <option>
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("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>`
When you select "The Godfather"
and submit, the console shows:
2015/12/05 21:18:42 Selected film: 1
When you select "The Godfather: Part II"
and submit, the console shows:
2015/12/05 21:18:45 Selected film: 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论