我希望通过单独点击+或 - 按钮来增加或减少每个产品,但问题是当我点击任何+按钮时...所有产品的价值都增加而不是一个,当我点击 - 按钮时相同。请帮助我如何做到这一点
jquery的
$(document).ready( function(){
$(".quantity-adder .add-action").click( function(){
if( $(this).hasClass('add-up') ) {
$("[name=quantity]",'.quantity-adder').val( parseInt($("[name=quantity]",'.quantity-adder').val()) + 1 );
}else {
if( parseInt($("[name=quantity]",'.quantity-adder').val()) > 1 ) {
$("input",'.quantity-adder').val( parseInt($("[name=quantity]",'.quantity-adder').val()) - 1 );
}
}
} );
});
HTML
<div class="product-extra">
<p>Product 1</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="40" />
</div>
</div>
<br />
<br />
<br />
<div class="product-extra">
<p>Product 2</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="44" />
</div>
</div>
<br />
<br />
<br />
<div class="product-extra">
<p>Product 3</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="48" />
</div>
</div>
<br />
<br />
<br />
<div class="product-extra">
<p>Product 4</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="55" />
</div>
</div>
<br />
<br />
<br />
答案 0 :(得分:4)
$(document).ready(function () {
$(".quantity-adder .add-action").click(function () {
if ($(this).hasClass('add-up')) {
var text = $(this).parent().parent().parent().find("[name=quantity]", '.quantity-adder')
text.val(parseInt(text.val()) + 1);
} else {
var text = $(this).parent().parent().parent().find("[name=quantity]", '.quantity-adder')
if (parseInt(text.val()) > 1) {
text.val(parseInt(text.val()) - 1);
}
}
});
});
我添加了.parent()
,然后找到要增加的正确文字
答案 1 :(得分:1)
您需要选择正确的控件并增加/减少该值。这可以通过使用最接近公共父div然后使用find来选择正确的控件来轻松完成。
$(document).ready(function () {
$(".quantity-adder .add-action").click(function () {
var ctrl = $(this).closest('.quantity-adder').find("[name=quantity]", '.quantity-adder');
if ($(this).hasClass('add-up')) {
$(ctrl).val(parseInt($(ctrl).val()) + 1);
} else {
if (parseInt($(ctrl).val()) > 1) {
$(ctrl).val(parseInt($(ctrl).val()) - 1);
}
}
});
});
或者,您可以将<input type='number' min='1' value='1' />
用于现代浏览器(不需要任何javascript)
答案 2 :(得分:1)
工作小提琴链接:https://jsfiddle.net/sjyq64mj/5/
$(document).ready( function(){
$(".quantity-adder .add-action").click( function(){
var qtyField = $(this).closest('.quantity-adder').find("[name=quantity]");
if( $(this).hasClass('add-up') ) {
qtyField.val( parseInt(qtyField.val()) + 1 );
}else {
if( parseInt(qtyField.val()) > 1 ) {
qtyField.val( parseInt(qtyField.val()) - 1 );
}
}
} );
});
答案 3 :(得分:0)
尝试closest()
:
//quantity text - box finder
var qty = function(el) {
return $(el).closest('div.quantity-adder').find('input[type=text][name=quantity]').eq(0);
};
var plus = function(e) {
var q = qty(this);
q.val(1 + (+q.val() || 0));
};
var minus = function(e) {
var q = qty(this);
var v = (+q.val() || 0) - 1;
if (1 > v)
v = 1;
q.val(v);
};
$(function() {
$('span.fa-plus').on('click', plus);
$('span.fa-minus').on('click', minus);
});
&#13;
.pull-left {
float: left;
}
span.fa-plus:after {
content: "+";
}
span.fa-minus:after {
content: "-";
}
span.add-action {
cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="product-extra">
<p>Product 1</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="40" />
</div>
</div>
<br />
<div class="product-extra">
<p>Product 2</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="44" />
</div>
</div>
<br />
<div class="product-extra">
<p>Product 3</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="48" />
</div>
</div>
<br />
<div class="product-extra">
<p>Product 4</p>
<div class="quantity-adder pull-left">
<div class="quantity-number pull-left">
<span>Qutantity</span>
<input type="text" name="quantity" size="2" value="1" />
</div>
<div class="quantity-wrapper pull-left">
<span class="add-up add-action fa fa-plus"></span>
<span class="add-down add-action fa fa-minus"></span>
</div>
<input type="hidden" name="product_id" size="2" value="55" />
</div>
</div>
&#13;