简体   繁体   English

遍历ID和数组jQuery

[英]Loop through ID and array jQuery

I have a checkout page that has some item numbers that show up on the page. 我有一个结帐页面,页面上显示了一些项目编号 I got all the item numbers and put them into an array. 我得到了所有的项目编号,并将它们放入一个数组中。 Each item on the page has an ID that increases by one (#item1, #item2) and class of .item . 页面上的每个项目都有一个ID,该ID增加一个(#item1,#item2).item类。

If the item matches an item number in the array then I need to hide or remove an edit link with the class of .edit-item and a Save For Later link with the class .save . 如果项目与数组中的项目编号匹配,那么我需要隐藏或删除类为.edit-item的编辑链接,以及一个类为.save的“以后保存”链接。

I went with children so as to not affect other items on the page but I am running into issues with the code below. 我和孩子一起去的时候是为了不影响页面上的其他项目,但是下面的代码遇到了问题。 Any advice on how to proceed? 关于如何进行的任何建议? I feel like I might have approached this wrong. 我觉得我可能已经走错了路。

var selectors = ["NMF19_N5N5K", "NMF19_N5N5E", "NMF19_N5N5N", "NMF19_N5N5L", "NMF19_N5N5C", "NMF19_N5N5F", "NMF19_N5N5M", "NMF19_N5N5Q", "NMF19_N5N5P", "NMF19_N5N5D"];
    var idNumber = 1;
    var i;

for (i = 0; i < selectors.length-1; i++) {

    if (jQuery('#item' + idNumber)) {
        jQuery('#item' + idNumber + ':contains(' + selectors[i] + ')' ).children()[8].children[0].children[2].children[1].children[0].children[2].remove();
        jQuery('#item' + idNumber + ':contains(' + selectors[i] + ')' ).children()[8].children[0].children[2].children[1].children[3].remove();
        idNumber++;
    }

}

While having the actual HTML would have been helpful the description along with the code provides an idea of what HTML structure might be. 尽管使用实际的HTML会有所帮助,但说明和代码一起提供了关于HTML结构可能是什么的想法。 Before providing a solution I will like to go over a few jQuery related things. 在提供解决方案之前,我将先介绍一些与jQuery相关的内容。

  1. $ is an alias of jQuery . $jQuery的别名。 $ is used in most example code online. 在线大多数示例代码中都使用$
  2. The expression jQuery(selector) will always be truthy. 表达式jQuery(selector)将始终是真实的。 To check if an element exists use jQuery(selector).length . 要检查元素是否存在,请使用jQuery(selector).length The expression results in 0 if the element does not exist, the count of matched elements otherwise. 如果该元素不存在,则表达式的结果为0 ,否则为匹配元素的计数。

The solution is to loop through all .item elements with jQuery.each() . 解决方案是使用jQuery.each()遍历所有.item元素。 Then use Array.some() and String.indexOf to find any of the item numbers from the list in each row. 然后使用Array.some()String.indexOf从每一行的列表中找到任何项目编号。 If found then remove the edit and save action buttons. 如果找到,则删除编辑和保存操作按钮。

 var itemNumbers = ["NMF19_N5N5K", "NMF19_N5N5E", "NMF19_N5N5N", "NMF19_N5N5L", "NMF19_N5N5C", "NMF19_N5N5F", "NMF19_N5N5M", "NMF19_N5N5Q", "NMF19_N5N5P", "NMF19_N5N5D"]; // Loop through all .item elements $('.item').each(function() { // An item reference, Ex: #item1, #item2 ... var $item = $(this); // The item row as text var itemAsText = $item.text(); // Check if this item row contains an item from the itemsNumbers list var matched = itemNumbers.some(function(itemNumber) { return itemAsText.indexOf(itemNumber) > -1; }); if (matched) { // Look for the edit and save elements within $item and delete them $item.find('.edit-item').remove(); $item.find('.save').remove(); } }); 
 ol { width: 400px } .item { margin-bottom: 5px; } .item button { margin-left: 5px; float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ol> <li id="item1" class="item">Item 1 - NMF19_N5N5N <button class="save">Save</button><button class="edit-item">Edit</button></li> <li id="item2" class="item">Item 2 - Custom5<button class="save">Save</button><button class="edit-item">Edit</button></li> <li id="item3" class="item">Item 3 - NMF19_N5N5F <button class="save">Save</button><button class="edit-item">Edit</button></li> <li id="item4" class="item">Item 4 - Custom4 <button class="save">Save</button><button class="edit-item">Edit</button></li> <li id="item5" class="item">Item 5 - NMF19_N5N5D <button class="save">Save</button><button class="edit-item">Edit</button></li> </ol> 

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

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