2017-04-21 86 views
0

我有以下的html代码: products获得从父节点父在JS

当我点击删除按钮从该产品的< LI>属性必须删除。

它曾与JS工作,但现在它没有这样做。 监听器:

cartList.on('click', '.delete-item', function(event){ 
    event.preventDefault(); 
    removeProduct($(event.target).parents('.product')); 
}); 

这是removeproduct功能,直到它崩溃了:

function removeProduct(product) { 
    clearInterval(undoTimeoutId); 
    cartList.find('.deleted').remove(); 
    var topPosition = product.offset().top - cartBody.children('ul').offset().top, 
    productQuantity = Number(product.find('.quantity').find('input').val()), 
    productTotPrice = Number(product.find('.price').text().replace('€', '')); 
} 

我得到的错误:

main.js:168 Uncaught TypeError: Cannot read property 'top' of undefined

所以他很可能无法获得该节点的产品但只读取“动作” - 作为父节点的第一个节点。但是,我怎么能存取权限的产品里属性删除整个行

+0

'cartBody.children( 'UL')偏移()''是undefined'。什么是'cartBody'?它是否有任何儿童'​​ul'元素? –

+2

如果您有问题需要显示HTML,请将其包含为文本,而不是图片,以便相关人员可以轻松地将其复制到答案中。 – nnnnnn

+0

html是用javascript编码的 – belgiums

回答

0

尝试使用下面的代码片段:

cartList.on('click', '.delete-item', function(event){ 
$(this).parents('.product').remove(); //this will remove the parent .product from the DOM 
}); 
+0

。你知道我是如何得到它的“productQuantity”,现在不工作.. – belgiums

+0

productQuantity = Number(product.find('。quantity')。find('input')。val()),在div“数量”内没有输入标签。 –