简体   繁体   English

为什么每次单击按钮都会成倍增加? (javascript ejs)

[英]Why are buttons being multiplied every time one is clicked? (javascript ejs)

It was supposed to be one red button or one blue button but every click on a blue button add 1 button on each line.它应该是一个红色按钮或一个蓝色按钮,但每次单击蓝色按钮都会在每行添加一个按钮。
And every click on a red button deletes 1 button on each line.并且每次点击红色按钮都会删除每行上的 1 个按钮。

image:图片:

在此处输入图像描述

<% for (var i=0; i < loadPosts.length; i++) { %>
    <tr>
        <td><%= loadPosts[i].name %></td>
        <td><%= loadPosts[i].price %></td>
        <td><%= loadPosts[i].id %></td>

        <td>
            <% for ( var j=0; j < homeProduct.length; j++ ) { %>
                <% if ( homeProduct[j].ProductId === loadPosts[i].id ) { %> 
                    <button  class="btn btn-danger"><a style="color: white; 
                        cursor: pointer; text-decoration: none;" 
                        href="/deleteFromHomePage/<%= 
                        loadPosts[i]._id%>">DeleteFromHomePage</a></button>
                <% } else { %>
                    <button  class="btn btn-primary"><a style="color: white; 
                         cursor: pointer; text-decoration: none;" 
                         href="/addToHomePage/<%= 
                         loadPosts[i]._id%>">AddToHomePage</a></button>
                <% } %>
            <% } %>   
        </td>   
        <td> 
            <button  class="btn btn-danger"><a style="color: white; 
                 cursor:pointer; text-decoration: none;" href="/delete/<%= 
                 loadPosts[i]._id%>">Delete</a></button>
        </td>
        
        
    </tr>
    
<% } %>

Your nested loop creates a button for each product.您的嵌套循环为每个产品创建一个按钮。 If the productID is the same as the post ID, it's a delete button, otherwise it's an add button.如果productID与post ID相同,则为删除按钮,否则为添加按钮。

I think what you actually want to do is check whether the post ID is in the homeProduct array.我认为您真正想要做的是检查帖子 ID 是否在homeProduct数组中。 If it is, you want a delete button;如果是,您需要一个删除按钮; if not, you want an add button.如果没有,您需要一个添加按钮。 You can use the some() function to tell if there's a match.您可以使用some()函数来判断是否有匹配项。

<% for (var i=0; i < loadPosts.length; i++) { %>
    <tr>
        <td><%= loadPosts[i].name %></td>
        <td><%= loadPosts[i].price %></td>
        <td><%= loadPosts[i].id %></td>

        <td>
            <% if (homeProduct.some(p => p.ProductId = loadPosts[i].id)) { %> 
                <button  class="btn btn-danger"><a style="color: white; 
                    cursor: pointer; text-decoration: none;" 
                    href="/deleteFromHomePage/<%= 
                    loadPosts[i]._id%>">DeleteFromHomePage</a></button>
            <% } else { %>
                <button  class="btn btn-primary"><a style="color: white; 
                     cursor: pointer; text-decoration: none;" 
                     href="/addToHomePage/<%= 
                     loadPosts[i]._id%>">AddToHomePage</a></button>
            <% } %>
        </td>   
        <td> 
            <button  class="btn btn-danger"><a style="color: white; 
                 cursor:pointer; text-decoration: none;" href="/delete/<%= 
                 loadPosts[i]._id%>">Delete</a></button>
        </td>
        
        
    </tr>
    
<% } %>

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

相关问题 单击时,事件监听器正在成倍增加 - eventListeners are being multiplied when clicked on 为什么每次单击按钮时都没有检查收音机 - Why are the radios not being checked every time I click the buttons 每次单击按钮时都在提交表单 - Form is being Submitted every time The button is clicked 如果在javascript中更改按钮单击按钮时如何运行某种方法? - How to run a certain method every time a button is clicked with changing buttons in javascript? 为什么我的 javascript 上的按钮在单击时没有响应? - Why are my buttons on javascript not responding when clicked? 在页面加载和每次单击某些按钮时运行jQuery函数 - Run jQuery Function on Page Load AND Every Time Certain Buttons Are Clicked 为什么 javascript 执行时间每次都不同? - Why is javascript execution time different every time? Javascript自动播放声音文件,当单击其中一个按钮时停止 - Javascript autoplay soundfile, stop when one of the buttons is clicked 如果只单击一个,如何避免更改所有按钮的颜色? JavaScript - How to avoid changing colors on all buttons if only one is clicked? JavaScript 如何检测javascript中2个按钮是否被依次点击? - How to detect whether 2 buttons are clicked one after the another in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM