donglu5612 2018-06-16 10:18
浏览 42
已采纳

如何找到所选提交按钮的ID

I have a page that is dynamically generated from my database. It basically reflects all the contents of a table.

 {{range .EquipmentList}}

<form method="POST" action="/cart">
<div class="row">
  <div class="col-1"></div>
  <div class="col-5">
     <p name="test"> {{.Name}} </p>
     <br>
     {{.Description}}
     <br>
     <button id="{{.Name}}" type="submit" class="btn btn-main btn-margin" ><b>Ausleihen</b></button> 
     {{if .Availability}}
      <input type="number" class="form-control main-input2 feld2" id="amount" name="amount" placeholder="Anzahl">
     <em class="status-text" style="color:green"> verfügbar </em>
     {{else}}
      <input type="number" class="form-control main-input2 feld2" id="amount" name="amount" placeholder="Anzahl">
     <em class="status-text"> entliehen </em>
     {{end}}
  </div>
  <div class="col-2"></div>
  <div class="col-2" style="margin-top: 30px">
     <img class="float-right img-responsive bild" src="{{.ImgPath}}" alt="Kann nicht gezeigt werden">
  </div>
  </div>

<div class="placeholder" style="height:50px"></div>

</form>
{{end}}

In the template, all devices from the DB are listed and provided with their own Submit button.

How can I find the fitting .Name entry to the triggered submit button?

I want to pass this value to my / cart located in the controller.

    func Cart(w http.ResponseWriter,    r   *http.Request)  {   

data := Data{ 
    Name: "Cart",
    Pages: []Page{
        {
            Title: "Meine Geräte", 
            Active: false,
            Link: "/my-equipment",
        },
        {
            Title: "Equipment", 
            Active: false,
            Link: "/equipment",
        },
        {
            Title: "Logout",
            Active: false,
            Link: "/logout",
        },
    },
}

if r.Method=="POST"{
 // SAVE .Name here
    tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
    tmpl.ExecuteTemplate(w, "base", data)
    }else{


        tmpl:= template.Must(template.ParseFiles("template/base_user.html", "template/cart.html"))
    tmpl.ExecuteTemplate(w, "base", data)
    }
}

Now I got it:

{{range .EquipmentList}}
<form action="/cart?id={{.ID}}" method="POST">
<a href="/cart?id={{.ID}}">{{.Name}}</a>
<button type="submit">
</button></form>
{{end}}

if iam using this code, i can use the id value with r.formValue("id") in my controller.

  • 写回答

1条回答 默认 最新

  • doufeixi6014 2018-06-16 11:05
    关注

    there are many ways but one of them is <form method="POST" action="/cart/{{.Name}}">

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)