如何使用ejs将一些JS代码包含到Bootstrap模式中?

时间:2019-06-13 16:07:06

标签: javascript jquery node.js bootstrap-4 ejs

我有一个ejs页面,该页面使用一些数据,包括通过<%= items [i] .id%>的循环以及添加或删除项目的按钮。一旦将删除功能转移到引导程序模式(创建删除确认对话框)中,循环就会停止工作,并且只有第一个数组项会被删除。我正在使用Bootstrap 4.0及其内置的JQuery。如果在模态之外执行删除,则可以正常工作。谢谢您的提示!

<% for (let i=0; i<items.length; i++) { %>
<div class="card w-100 p-3">
  <form method="post" >
    <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
      <div class="card-header">
        <%= items[i].name  %>
      </div>
      <div class="card-body">
        <p class="card-text"> <%= items[i].desc  %> </p>
      </div>
      <button formaction="/project" type="submit" class="btn btn-outline-dark" name="editButton" value="<%= items[i].id  %>">Edit</button>
      <button type="button" class="btn btn-outline-danger" data-toggle="modal" data-target="#deleteModal">Delete</button>

    </div>
  </form>
</div>


<!-- Delete Modal -->
 <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <form class="form-control" action="/delete" method="post">
          <p>Are you sure you want to delete <%= items[i].name  %> ? </p>

        <button type="submit" class="btn btn-outline-danger" name="deleteButton" value="<%= items[i].id  %>">Delete</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        </form>

      </div>
    </div>
  </div>
</div> 

<% } %>

1 个答案:

答案 0 :(得分:0)

所有<div class="model...具有相同的id属性。当您单击删除按钮时,只会打开第一个模态。在后缀<%= i %>上添加id作为后缀:

<% for (let i=0; i<items.length; i++) { %>
    <div class="card w-100 p-3">
        <form method="post">
            ...
            <button data-target="#deleteModal<%= i %>" type="button" class="btn btn-outline-danger" data-toggle="modal" >Delete</button>
        </form>
    </div>

    <!-- Delete Modal -->
    <div id="deleteModal<%= i %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        ...
    </div> 
<% } %>