简体   繁体   English

每当我点击删除按钮时,总是第一个项目被删除,而不是我要删除的特定项目

[英]always 1st item get deleted when ever i tap on the delete button not the particular item which i want to delete

 app.get('/delete', function(req, res, next) { var id = req.query.id; console.log(id); MongoClient.connect(dburl, function(err, db) { if(err) { throw err; } db.collection('shopping_list', function(err, products) { products.deleteOne({_id: mongodb.ObjectID(id)}); if (err){ throw err; }else{ //console.log("deleted"); db.close(); } }); }); }); 
 <body> <h1> Shopping List </h1> <img src="images/cart.png" id="cart"> <div class="text"> <input type="search" placeholder="Find" id="find"/> <a href="http://www.google.com"><input type="submit" id="find" value="Search" style="background-color: #a3a3c2 ;color:white; width:170px;"></a> </div> <form action="/add" method="POST"> <div class="f"> <a href="http://www.facebook.com"><img src="images/fc.jpg" class="follow"></a> <a href="http://www.twitter.com"><img src="images/twt.jpg" class="follow"></a> <a href="http://www.instgram.com"><img src="images/ins1.jpg" class="follow"></a> </div> <div class="container"> <div class="row"> <div class="col-md-4"> <label style="margin-left: 20px;">Shopping item</label>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; <label>Quantity</label> </div> </div> <div class="row"> <div class="col-md-8"> <input type="text" id="item" name="item"/>&emsp;&emsp;&emsp;&emsp; <input type="text" id="quan" name="quan"/>&emsp;&emsp;&emsp;&emsp; <input type="submit" id="submit" value="Add item" style="background-color: #a3a3c2 ; color:white; width:170px;" /> </div> </div> </div> </form> <div class="container"> <div class="row"> <div class="col-md-12"> <table id="tbody"> <tr> <th>Item</th> <th>Quantity</th> </tr> </table> </div> </div> </div> <button id="btn" style="background-color: #a3a3c2 ; color:white; width:170px; margin-left:807px; margin-top:30px;">Clear</button> <script> $(function() { // GET $.ajax({ url: '/render', type: 'get', dataType: 'json', success: function(data) { for (var i = 0; i < data.length; i++) { var trHTML = ''; trHTML += '<tr>'; trHTML += '<td style="display:none" id="id">' +data[i]._id +'</td>'; trHTML += '<td >' + data[i].item + '</td>'; trHTML += '<td >' + data[i].quantity + '</td>'; trHTML += '<td >' + '<button class="delete-button" style="background-color: #a3a3c2 ; color:white; width:170px; margin-left:370px;">Delete</button>' + '</td>'; trHTML += '</tr>'; $('#tbody').append(trHTML); } } }); $(document).ready(function(){ $('table').on('click', '.delete-button', function() { alert("Confirm delete"); var id=$('#id').html(); console.log(id); //var rowEl = $(this).closest('tr'); //var id = rowEl.find('.id').text();*/ $.ajax({ url: '/delete', type: 'get', dataType: 'json', data:{id:id}, success: function(data) { alert("deleted"); //$(this).closest('tr').remove(); } }); }); }); 
always 1st item get deleted when ever i tap on the delete button not the particular item which i want to delete 每当我点击删除按钮时,总是第一个项目被删除,而不是我要删除的特定项目

every time item which is on the top of the database get deleted when i use the delete button but i want particular item to be delete. 每当我使用删除按钮但我希望删除特定项目时,位于数据库顶部的每次项目都会被删除。 as i provide individual delete button for each item also im facing the same problem. 因为我为每个项目提供单独的删除按钮,所以我也面临相同的问题。

This line is the problem: 这行是问题所在:

var id=$('#id').html();

Because you have multiple cells with the ID of 'id', this assignment picks the first one. 因为您有多个ID为“ id”的单元格,所以此分配选择了第一个。 Change it to this: 更改为此:

var id = $(this).parent().siblings('#id').text();

to get the contents of the 'id' cell on the same row as your delete button. 在与删除按钮相同的行中获取“ id”单元格的内容。

暂无
暂无

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

相关问题 当我想删除列表中的一项时,所有内容都会被删除 Expo, React Native - When i want to delete one item in my list, everything gets deleted Expo, React Native 删除项目时自动单击项目 - Clicking on an item automatically when I delete an item 为什么@keyfame animation 属性欺骗到下面的列表项? 我想使用 React js 删除要在单击按钮时删除的列表项 - Why the @keyfame animation property tricking down to below list item? I want to delete a list item to be deleted upon button click using React js 我想从本地存储中删除一个项目 - i want to delete an item from local storage 如何在菜单顶部添加标题和关闭按钮(在 material-ui 中单击时,在按钮菜单中的第一项上方? - How do I add a title and close button to the top of a menu (above the 1st item in a button menu when clicked in material-ui? 无法删除数组项,错误的项总是被删除 - cannot delete array item, the wrong item always gets deleted 我想做一个条件,如果我从第一个下拉列表中选择这个项目,它会只显示我在第二个下拉列表中选择的项目 - I want to make a condition in which if I select this item from 1st drop down show it shows me only selected items in 2nd drop down 在第二个按钮上单击,我希望第一个以及第二个按钮被着色 - onclick on 2nd button,i want 1st as well as 2nd button to be get coloured 如何删除存储在本地存储中的特定项目? - How can i delete particular item stored inside local storage? 如何使同样使用列表项创建的按钮起作用? 我希望它删除数组的当前项。 每个项目都有自己的按钮 - How do i make the button that's also created with the list item work? I want it to delete the current item of the array. Each item has it own button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM