doujing1858 2014-09-05 12:20
浏览 174
已采纳

Golang模板中的HTML表单提交

I am using the go language to submit an HTML form using the go-template. Getting some weird results.

The basic idea is that I have a data structure called Page containing a few elements. The template is populated with an array of Pages. Inside the template I iterate through each Page and display its contents. Each of these contents are embedded inside an HTML form with its respective link. Once the link is clicked it will submit the respective form.

The code snippet is as follows:

{{range $index, $element := .Pages}}                                                                            
  <form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
    //displaying elements from each page                    

    <div id="more">
      <input name="query" type="hidden" value="{{printf "%s" .Title}}">                         
      <a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
    </div>

  </form>
{{end}}

The code mostly works with one little problem. The id and the name attributes generate outputs as expected such as: readMore0, readMore1, etc.

The problem is at the "a" tag where the onclick attribute is populated with this: document.readMore 0 .submit(), document.readMore 1 .submit(), etc. Note the space surrounding 0, 1. With this, the respective form is not being found when the link is clicked.

I can't figure out the reason of this.

Any help will be highly appreciated.

Thanks, Ripul

  • 写回答

2条回答 默认 最新

  • dtnbjjq51949 2014-09-05 13:38
    关注

    Do you have a working example to reproduce? I tried locally and it works as expected:

    http://play.golang.org/p/ZnM0RqIMfL

    package main
    
    import (
            "os"
            "text/template"
    )
    
    type Page struct {
            Title string
    }
    
    func main() {
            template.Must(template.New("test").Parse(`
    {{range $index, $element := .Pages}}
      <form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
        //displaying elements from each page
    
        <div id="more">
          <input name="query" type="hidden" value="{{printf "%s" .Title}}">
          <a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
        </div>
    
      </form>
    {{end}}
    `)).Execute(os.Stdout, struct{ Pages []Page }{Pages: []Page{
                    {Title: "page1"}, {Title: "page2"},
            }})
    }
    

    Result:

     <form action="/detailNews" id="readMore0" method="post" name="readMore0">
        //displaying elements from each page
    
        <div id="more">
          <input name="query" type="hidden" value="page1">
          <a href="#" onclick="document.readMore0.submit()">Read More</a>
        </div>
    
      </form>
    
      <form action="/detailNews" id="readMore1" method="post" name="readMore1">
        //displaying elements from each page
    
        <div id="more">
          <input name="query" type="hidden" value="page2">
          <a href="#" onclick="document.readMore1.submit()">Read More</a>
        </div>
    
      </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码