英文:
changing a forms action attribute dynamically in a go template
问题
目前,表单的 action 属性被硬编码为 id=2。当用户按下提交按钮时,我该如何动态设置它为用户选择的 ID?
<p>选择一个组</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">艺术家:</label>
<select id="selection" name="selection">
<option value=1>Queen</option>
<option value=2>Pink Floyd</option>
<option value=3>Scorpions</option>
</select>
<input type="submit" value="提交" />
</form>
Go 模板:
<p>选择一个组</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">艺术家:</label>
<select id="selection" name="selection">
{{range .}}
<option value={{.Id}}>{{.Name}}</option>
{{end}}
</select>
<input type="submit" value="提交" />
</form>
请注意,以上是翻译的内容,不包括代码部分。
英文:
Currently the forms action attribute is hardcoded to the id=2
How would I dynamically set it to the ID chosen by the user when pressing submit.
<p>Select a group</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">Artists:</label>
<select id="selection" name="selection">
<option value=1>Queen</option>
<option value=2>Pink Floyd</option>
<option value=3>Scorpions</option>
</select>
<input type = "submit" value ="submit" />
</form>
Go template:
<p>Select a group</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">Artists:</label>
<select id="selection" name="selection">
{{range .}}
<option value={{.Id}}>{{.Name}}</option>
{{end}}
</select>
<input type = "submit" value ="submit" />
</form>
答案1
得分: 1
你使用了 method="GET" 的表单方法,并按照 @cerise 的建议进行了更改。
英文:
Used form method="GET" and made the changes suggested by @cerise
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论