简体   繁体   English

无法使按钮在EJS模板中工作

[英]Unable to get button to work in EJS template

I have a loop that populates a page with several items. 我有一个循环,其中填充了多个项目的页面。 Each item has: 每个项目都有:

  • an image 一个图像
  • text 文本
  • button 纽扣
  • a unique ID associated with it 与之关联的唯一ID

As a test, I wrote a function to return the ID and the plan name when the button is clicked. 作为测试,我编写了一个函数,当单击按钮时返回ID和计划名称。 However, only the ID and plan name of the first item is returned for all the items in the list. 但是,对于列表中的所有项目,仅返回第一个项目的ID和计划名称。 When I look at the HTML, the correct unique IDs are populated in the hidden fields, so I am having a hard time figuring out why the correct IDs are being returned when clicked. 当我查看HTML时,正确的唯一ID会填充在隐藏字段中,因此我很难弄清楚为什么单击时会返回正确的ID。

Please see the code below: 请参见下面的代码:

EJS: EJS:

<% for (var i = 0; i < plans.length; i++) {%>
  <div class="col-md-4" data-shuffle="item" data-groups="audio,video">
     <form>
        <a class="hover-move-up" href="#">
           <img src="/img/portfolio/1.jpg" alt="screenshot">
           <div class="text-center pt-5">
               <h5 class="fw-5001 mb-0"><%= plans[i].plan_name %></h5>
               <small class="small-5 text-lightest text-uppercase ls-2"><%= plans[i].regular_plan_cost %> | <%= plans[i].huddl_plan_cost %></small> 
                  <br>
                  <input id="planID" name="ID" type="hidden" value="<%= plans[i]._id %>">
                  <input id="planName" name="planName" type="hidden" value="<%= plans[i].plan_name %>">
                  <button type="submit" class="btn btn-primary">Join</button>
              </form>
                </div>
              </a>
            </div>

          <% } %>

JS: JS:

var ID = document.getElementById("planID").value;
var planName = document.getElementById("planName").value;

$('button').on('click', function() {
  alert(ID + ' ' + planName);
})

Any help figuring this out will be appreciated. 任何帮助弄清楚这一点将不胜感激。

Take look at the form tag you are having, it is not closet correctly. 看一下您所拥有的表单标签,它没有正确放入壁橱。 You need to move it under the div and the a tag. 您需要将其移动到div和a标签下。 Perhaps this will fix that. 也许这将解决此问题。

use this snippet. 使用此代码段。

    <div class="text-center pt-5">
           <h5 class="fw-5001 mb-0"><%= plans[i].plan_name %></h5>
           <small class="small-5 text-lightest text-uppercase ls-2"><%= plans[i].regular_plan_cost %> | <%= plans[i].huddl_plan_cost %></small> 
              <br>
              <input id="planID_<%= i %>" name="ID" type="hidden" value="<%= plans[i]._id %>">
              <input id="planName_<%= i %>" name="planName" type="hidden" value="<%= plans[i].plan_name %>">
              <button data-ref="<%= i %>" class="btn btn-primary">Join</button>
          </form>
            </div>
          </a>
        </div>

I just added an incrementor to the IDS: 我刚刚向IDS添加了一个增量器:

<input id="planID_<%= i %>"
<input id="planName_<%= i %>"
<button data-ref="<%= i %>"

So, now you have your unique ids. 因此,现在您有了自己的唯一ID。 Do you also need the javascript to "alert"? 您还需要JavaScript来“提醒”吗?

Here is some additional code. 这是一些其他代码。 I didn't test this and my jQuery is rusty, but hopefully this will give you what you need 我没有对此进行测试,但我的jQuery还是生锈的,但是希望这会给您您所需要的

Javascript: Javascript:

$('button').each(function() {
  $(this).on('click', function() {
     myFN($(this).attr("data-ref"));
  })
});

function myFN(val) {
  var ID = document.getElementById("planID_" + val).value;
  var planName = document.getElementById("planName_" + val).value;
  console.log("VALUES: ", ID, planName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM