如果單擊數量,我正在嘗試使用類價格獲取元素的值。我繼續使用 next() ,find(),最接近() 不確定。它在這一點上要求應該更快。
$(function() {
$('input.quantity').click(function() {
var test = $(this).parent('.qty-price').closest('.price').html();
alert(test);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="product">
<a href="/" class="product-link">
<span class="product-details">
<h3>Test Description</h3>
<span class="qty-price">
<span class="qty">
<input type="number" rel="0" class="form-control text-center quantity" value="1" min="0">
</span>
<span class="price">$5.00</span>
</span>
</span>
</a>
<a href="#remove" class="remove-button"><span class="remove-icon">X</span></a>
</li>
uj5u.com熱心網友回復:
假設您的輸入沒有像您在評論中所說的那樣被禁用,您可以使用以下內容。請注意,由于您的輸入位于錨點內,因此您需要阻止單擊事件使 DOM 冒泡。
$(function() {
$('input.quantity').click(function(e) {
e.stopPropagation();
var test = $(this).parent('.qty').next('.price').text();
alert(test);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="product">
<a href="/" class="product-link">
<span class="product-details">
<h3>Test Description</h3>
<span class="qty-price">
<span class="qty">
<input type="number" rel="0" class="form-control text-center quantity" value="1" min="0" >
</span>
<span class="price">$5.00</span>
</span>
</span>
</a>
<a href="#remove" class="remove-button"><span class="remove-icon">X</span></a>
</li>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478987.html
上一篇:頁面加載后獲取URL引數
