我试图通过jQuery进行增量递减。
jQuery(function() {
jQuery("div.inputQty").append('<div class="inc add">+</div><div class="dec add">−</div>');
jQuery(".add").click(function() {
var jQueryadd = jQuery(this);
var oldValue = jQueryadd.parent().find("input").val();
var newVal = 0;
if (jQueryadd.text() == "›") {
newVal = parseFloat(oldValue) + 1;
// AJAX save would go here
} else {
// Don't allow decrementing below zero
if (oldValue > 1) {
newVal = parseFloat(oldValue) - 1;
// AJAX save would go here
}
if(oldValue == 1){
newVal = parseFloat(oldValue);
}
}
jQueryadd.parent().find("input").val(newVal);
});
});
<div class="inputQty">
<span class="up">+</span>
<input type="text" maxlength="6" name="oa_quantity" class="input-quantity" value="1"/>
<span class="down">−</span>
</div>
但它不起作用。请帮我解决这个问题
Here是演示
答案 0 :(得分:0)
你可以这样尝试
jQuery(function() {
jQuery("div.inputQty").append('<div class="inc add">+</div><div class="dec add">−</div>');
jQuery(".add, .up, .down").click(function() {
var jQueryadd = jQuery(this);
var oldValue = jQueryadd.parent().find("input").val();
var newVal = 0;
if (jQueryadd.text() == "+") {
newVal = parseFloat(oldValue) + 1;
// AJAX save would go here
} else {
// Don't allow decrementing below zero
if (oldValue > 1) {
newVal = parseFloat(oldValue) - 1;
// AJAX save would go here
}
if(oldValue == 1){
newVal = parseFloat(oldValue);
}
}
jQueryadd.parent().find("input").val(newVal);
});
});
我认为它对你有帮助
答案 1 :(得分:0)
您忘了在测试中加载jQuery库。
此外,只要您已经拥有jQuery("div.inputQty").append('<div class="inc add">+</div><div class="dec add">−</div>');
和up
跨度,就不需要此行down
。
工作的js代码是:
jQuery(function() {
jQuery('.add').on('click', function() {
var jQueryadd = jQuery(this);
var oldValue = jQueryadd.parent().find("input").val();
var newVal = 0;
if (jQueryadd.hasClass('up')) {
newVal = parseFloat(oldValue) + 1;
// AJAX save would go here
} else {
// Don't allow decrementing below zero
if (oldValue > 1) {
newVal = parseFloat(oldValue) - 1;
// AJAX save would go here
}
if(oldValue == 1){
newVal = parseFloat(oldValue);
}
}
jQueryadd.parent().find("input").val(newVal);
});
});
和HTML:
<div class="inputQty">
<span class="up add">+</span>
<input type="text" maxlength="6" name="oa_quantity" class="input-quantity" value="1"/>
<span class="down add">−</span>
</div>
请参阅working demo
答案 2 :(得分:0)
如果要将事件添加到动态新元素中。在这种情况下,您必须使用delegated event
jQuery(".inputQty").on('click', '.add', function() {
//will work with dynamic elements
}
JsFiddle:http://jsfiddle.net/wLvpavug/5/
答案 3 :(得分:0)
我在var&#34; oldValue&#34;中添加了parseFloat .click函数的行和lastLine
jQuery(function() {
jQuery("div.inputQty").append('<div class="inc add">ADDDDDDDDD</div><div class="dec add">−</div>');
jQuery(".add").click(function() {
var jQueryadd = jQuery(this);
var oldValue = parseFloat(jQueryadd.parent().find("input").val());
console.log(oldValue);
var newVal = 0;
if (jQueryadd.text() == "›") {
newVal = parseFloat(oldValue) + 1;
// AJAX save would go here
} else {
// Don't allow decrementing below zero
if (oldValue > 1) {
newVal = parseFloat(oldValue) - 1;
// AJAX save would go here
}
if(oldValue == 1){
newVal = parseFloat(oldValue);
}
}
jQuery(".input-quantity").val(newVal);
});
});
检查出来&gt;&gt; http://jsfiddle.net/ChoHongRae/nqk6ns4v/