简体   繁体   English

如何使用 onclick function 更改 ejs 变量

[英]How to use onclick function to change ejs variable

I am creating a web application using Node, Express and ejs.我正在使用 Node、Express 和 ejs 创建一个 web 应用程序。 On the same page, I generate a set of buttons (first table below) and a set of corresponding hidden div s (second table below).在同一页面上,我生成了一组按钮(下面的第一个表)和一组相应的隐藏div (下面的第二个表)。 When a button is clicked I want to display the corresponding div .单击按钮时,我想显示相应的div

<% for(var i = 0; i < stream.length; i++){ %>
<tr>
 <th scope="row"><%= stream[i].streamid %></th>
 <td><%= stream[i].streamname %></td>
 <td><%= stream[i].streamername %></td>
 <td> 
  <button
    class="btn btn-outline-success"
    data-toggle="modal"
    data-target="#streamDetailsModal"
    id="detailsstream"
    onclick="<% ctr = i %>"
  >
  Details
  </button>
 </td>
 <td>
  <a
   class="btn btn-outline-danger"
   href="/stream/delete/<%= stream[i].streamid %>"
  >
  ⊖ Remove
  </a>
 </td>
</tr>
<% } %>
<% for(var i = 0; i < stream.length; i++){ %>
<tr>
  <th scope="row"><%= stream[i].streamid %></th>
  <td><%= stream[i].streamname %></td>
  <td><%= stream[i].streamername %></td>
  <td>
   <button
    class="btn btn-outline-success"
    data-toggle="modal"
    data-target="#streamDetailsModal"
    id="detailsstream"
    data-ctr="<%= i %>"
   >
   Details
  </button>
 </td>
 <td>
  <a
   class="btn btn-outline-danger"
   href="/stream/delete/<%= stream[i].streamid %>"
  >
  ⊖ Remove
 </a>
</td>
</tr>
<% } %>

I need the counter in first loop to change while the user clicks on the details button from the second loop.当用户单击第二个循环中的详细信息按钮时,我需要在第一个循环中更改计数器。

If you do wish to go with the above logic... the possible way will be to make a server call on the button click, compute the value on the server and the re render the page on the front end.如果您确实希望使用上述逻辑 go ... 可能的方法是在按钮单击时进行服务器调用,计算服务器上的值并在前端重新呈现页面 Browser Javascript cannot change or manipulate the ejs logic or variable.浏览器 Javascript 无法更改或操作 ejs 逻辑或变量。 That is done on the node server.这是在节点服务器上完成的。

The normal way to implement such functionality is to do something like this instead:实现此类功能的正常方法是执行以下操作:

in your first loop generating buttons, add each button a handler that shows the div with some unique id (like streamDetailsModal12 for button 12);在您的第一个循环生成按钮中,为每个按钮添加一个处理程序,该处理程序显示具有一些唯一 id 的 div(如按钮 12 的streamDetailsModal12 ); if you use bootstrap or something similar, you don't even have to implement the handler如果您使用引导程序或类似的东西,您甚至不必实现handler

  <button
    class="btn btn-outline-success"
    data-toggle="modal"
    data-target="#streamDetailsModal<% i %>"
    id="detailsstream"
    onclick="handler(event)"
  >

and to each div you'd like to show on button click you add corresponding ids:并为您希望在按钮上显示的每个div添加相应的 ID:

<div id="streamDetailsModal<% i %>"
> some content </div>

If you implement handler yourself, you have to grab the id from event.target (which is your button holding data-target attribute with the id you want) and then find the element and toggle it's display property or something else you'd like to control visibility.如果您自己实现handler ,则必须从event.target中获取 id(这是您的按钮,其中包含您想要的 id 的data-target属性),然后找到该元素并切换它的display属性或您想要的其他东西控制可见性。

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

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