[英]Calculate value of text input where checkbox is selected
我试图获取文本输入的值,在该文本输入旁边的选择框被选中。
我必须以某种方式将复选框和文本输入结合在一起,因此它只计算选择了同一表行上的复选框的文本输入的值。
还有一个问题是,当我的任何一个复选框被选中时,它们的任何一个都不具有'checked'属性,因此我也无法定位那些:checked的属性。
我怀疑到目前为止,我的方法有点过高,因此欢迎提出任何建议。
http://rlpetproducts2.designlocker.co.uk/index.php?route=product/product&product_id=251
$('table.table-bordered input[type="checkbox"]').change(function () {
$(this).toggleClass('jordan');
});
$('#button-cart').hover(function () {
var sum = 0;
$('table.table-bordered input[type="checkbox"]:checked').each(function() {
$('table.table-bordered input[type="text"].jordan').each(function() {
sum += Number($(this).val());
});
});
console.log(sum);
});
我们的目标 :“添加到购物篮”点击一下,即可选定选项的至少一个选择(其它警报),如果选择一个,计算从行内输入字段总量 (其中选择复选框)(现在简单提醒数量总计)。
在此示例中,要警告的值为5。
未经测试,但它应该像这样工作:
$('#button-cart').hover(function () {
var parent, sum = 0;
$('table.table-bordered input[type="checkbox"]:checked').each(function() {
// get current row
parent = $(this).closest('tr');
// in row find input and add the value
sum += parseFloat(parent.find('input[type="text"].jordan').val());
});
console.log(sum);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.