The problem with this code is that the closest() function returns only the first element that matches the specified selector, not a collection of elements. Therefore, using .each() on the result of closest() is unnecessary and incorrect.\n\nTo fix this, you can simply remove the .each() function and directly use $('#'+item_id).closest('.qcart_item') to target the closest element with the class .qcart_item. Here's the updated code:\n\n\nvar validate = ''; \nvar qq_order_items = [];\n\nvar closestQCart = $('#'+item_id).closest('.qcart_item');\n// use closestQCart as needed\n\n\nThis code will find the closest element with the class .qcart_item to the element with the specified item_id.

jQuery .closest() Function: Correcting Code for Finding Closest Element

原文地址: https://www.cveoy.top/t/topic/qc8i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录