我認爲這是你在找什麼。您可以使用eq()獲取特定索引,並使用index獲取當前索引。既然你根據你的其他結構化的.cart_product
DIV div
你可以得到的點擊div
目前指數並用它來顯示/隱藏相應的div
$(document).ready(function() {
var num;
$('.up').click(function() {
num = parseInt($(this).siblings('.product').text());
$(this).prevAll('.product').text(num + 1);
$('.cart_product').eq($(this).closest('.product_box').index()).show();// <-- on up click you will only ever show a product
});
$('.down').click(function() {
num = parseInt($(this).siblings('.product').text());
if (num > 0) {
$(this).siblings('.product').text(num - 1);
}
if (parseInt($(this).siblings('.product').text()) == 0) { <-- check if current val in div is == 0
$('.cart_product').eq($(this).closest('.product_box').index()).hide(); <-- if it is the hide the .cart_product div with the same index as the current "div"
} else {
$('.cart_product').eq($(this).closest('.product_box').index()).show();
}
});
});
http://jsfiddle.net/7XnXF/12/
完美,謝謝! – 2012-07-21 14:29:46
我已經使用此代碼,我將如何訪問.cart_product div內的另一個div?我需要更新項目的數量,以便與頁面左側的項目數量相匹配。我有一個新的jsfiddle來幫助解釋。我在代碼中添加了第7行,但它沒有正確訪問我需要更新的div。 http://jsfiddle.net/richcoy/MHMVC/ – 2012-07-21 15:00:13