2 回答

TA貢獻1895條經(jīng)驗 獲得超3個贊
您可以嘗試使用 jQuery.closest()
并按.find()
以下方式定位特定元素:
var?link?=?$(this).closest('.add-links').find('.ajax_add_to_cart');
然后分別增加/減少加/減的值。
演示:
jQuery(function($){
? ??
? ? $('.plus').on('click', function(e) {
? ? ? ? var val = parseInt($(this).prev('input').val());
? ? ? ? $(this).prev('input').val(val + 1).change();? ? ? ??
? ? ? ??
? ? ? ? console.clear();//test, clear the console
? ? ? ? var link = $(this).closest('.add-links').find('.ajax_add_to_cart');
? ? ? ? var v =? (+link.attr('data-quantity')) + 1;
? ? ? ? link.attr('data-quantity', v);
? ? ? ? console.log('Current data-quantity: ' + link.attr('data-quantity'));//test
? ? });
? ??
? ? $('.minus').on('click', function(e) {
? ? ? ? var val = parseInt($(this).next('input').val());
? ? ? ? if (val !== 0) {
? ? ? ? ? ? $(this).next('input').val(val - 1).change();
? ? ? ? ? ??
? ? ? ? ? ? console.clear(); //test, clear the console
? ? ? ? ? ? var link = $(this).closest('.add-links').find('.ajax_add_to_cart');
? ? ? ? ? ? var v =? link.attr('data-quantity') - 1;
? ? ? ? ? ? link.attr('data-quantity', v);
? ? ? ? ? ? console.log('Current data-quantity: ' +? link.attr('data-quantity')); //test
? ? ? ? }? ? ? ? ? ? ? ? ? ? ??
? ? });
? ??
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="add-links clearfix">
? ? <div class="quantity buttons_added">
? ? ? ? <button type="button" value="-" class="minus">-</button>
? ? ? ? <input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
? ? ? ? <button type="button" value="+" class="plus">+</button>
? ? </div>
? ? <a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>
<div class="add-links clearfix">
? ? <div class="quantity buttons_added">
? ? ? ? <button type="button" value="-" class="minus">-</button>
? ? ? ? <input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
? ? ? ? <button type="button" value="+" class="plus">+</button>
? ? </div>
? ? <a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>
<div class="add-links clearfix">
? ? <div class="quantity buttons_added">
? ? ? ? <button type="button" value="-" class="minus">-</button>
? ? ? ? <input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
? ? ? ? <button type="button" value="+" class="plus">+</button>
? ? </div>
? ? <a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>

TA貢獻1780條經(jīng)驗 獲得超1個贊
你可以在這里更改為$(this).parents('.add-links').find('a.ajax_add_to_cart')..
jQuery(function($){
$('.plus').on('click', function(e) {
var val = parseInt($(this).prev('input').val());
$(this).prev('input').val(val + 1).change();
});
$('.minus').on('click', function(e) {
var val = parseInt($(this).next('input').val());
if (val !== 0) {
$(this).next('input').val(val - 1).change();
}
});
$('.add-links').on('change', '.qty', function(event) {
$(this).parents('.add-links').find('a.ajax_add_to_cart').attr('data-quantity', + $(this).val());
});
$('.qty').change();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="add-links clearfix">
<div class="quantity buttons_added">
<button type="button" value="-" class="minus">-</button>
<input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
<button type="button" value="+" class="plus">+</button>
</div>
<a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>
<div class="add-links clearfix">
<div class="quantity buttons_added">
<button type="button" value="-" class="minus">-</button>
<input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
<button type="button" value="+" class="plus">+</button>
</div>
<a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>
<div class="add-links clearfix">
<div class="quantity buttons_added">
<button type="button" value="-" class="minus">-</button>
<input type="number" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" size="4" inputmode="numeric">
<button type="button" value="+" class="plus">+</button>
</div>
<a href="#" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart">Add to cart</a>
</div>

TA貢獻1942條經(jīng)驗 獲得超3個贊
.add-links
在事件處理程序中,您可以使用 找到包含事件目標元素的特定元素$(this).closest(".add-links")
。所以:
$('.add-links').on('change', '.qty', function(event) { $(this).closest(".add-links").find('a.ajax_add_to_cart').attr('data-quantity', + $(this).val()); });
添加回答
舉報