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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?