计算选中复选框的文本输入值

时间:2016-02-21 12:03:43

标签: javascript jquery checkbox

我正在尝试获取文本输入的值,其中选择了旁边的选择框。

我必须以某种方式将复选框和文本输入连接在一起,因此它只知道选择同一表格行上复选框的文本输入值。

问题还在于,我的所有复选框都没有获得“已检查”的属性。当被选中时,我也无法定位那些:经过检查。

我怀疑到目前为止我的方法有点过分,所以欢迎任何建议。

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);
});

目标:on'添加到购物篮'单击,选中至少一个选定的选项(否则为警报),如果选择了一个,则从内联输入字段(选中复选框)计算总数量(现在只需提醒数量总计)。

enter image description here

在此示例中,要提醒的值为5。

1 个答案:

答案 0 :(得分:1)

未经测试但它应该像这样工作:

$('#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);
});
相关问题