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条)

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案