changing a forms action attribute dynamically in a go template

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

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.

changing a forms action attribute dynamically in a go template

&lt;p&gt;Select a group&lt;/p&gt;

      &lt;form method=&quot;POST&quot; action=&quot;/artists/id?id=2&quot;&gt;
      &lt;label for=&quot;selection&quot;&gt;Artists:&lt;/label&gt;
      &lt;select id=&quot;selection&quot; name=&quot;selection&quot;&gt;
            
            &lt;option value=1&gt;Queen&lt;/option&gt;
            
            &lt;option value=2&gt;Pink Floyd&lt;/option&gt;
            
            &lt;option value=3&gt;Scorpions&lt;/option&gt;
            
         
            
      &lt;/select&gt;
      &lt;input type = &quot;submit&quot; value =&quot;submit&quot; /&gt;
      &lt;/form&gt;

Go template:

&lt;p&gt;Select a group&lt;/p&gt;
      &lt;form method=&quot;POST&quot; action=&quot;/artists/id?id=2&quot;&gt;
      &lt;label for=&quot;selection&quot;&gt;Artists:&lt;/label&gt;
      &lt;select id=&quot;selection&quot; name=&quot;selection&quot;&gt;
            {{range .}}
            &lt;option value={{.Id}}&gt;{{.Name}}&lt;/option&gt;
            {{end}}
      &lt;/select&gt;
      &lt;input type = &quot;submit&quot; value =&quot;submit&quot; /&gt;
      &lt;/form&gt;

答案1

得分: 1

你使用了 method="GET" 的表单方法,并按照 @cerise 的建议进行了更改。

英文:

Used form method="GET" and made the changes suggested by @cerise

huangapple
  • 本文由 发表于 2022年1月23日 00:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/70815053.html
匿名

发表评论

匿名网友

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

确定