これがあなたが探しているものだと思います。eq()を使用して特定のインデックスを取得し、インデックスを使用して現在のインデックスを取得できます。.cart_product
他のdivに従って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/