简体   繁体   English

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

[英]Calculate value of text input where checkbox is selected

I am trying to get the value of a text input where the select box next to it is selected. 我试图获取文本输入的值,在该文本输入旁边的选择框被选中。

I've got to somehow join the checkbox and text-input together, so it knows only calculate the value of the text input where the checkbox on the same table row is selected. 我必须以某种方式将复选框和文本输入结合在一起,因此它只计算选择了同一表行上的复选框的文本输入的值。

An issue is also the fact that none of my checkboxes get the attribute of 'checked' when selected either, so I've not been able to target those which are :checked too. 还有一个问题是,当我的任何一个复选框被选中时,它们的任何一个都不具有'checked'属性,因此我也无法定位那些:checked的属性。

I suspect my method so far is going a little too over the top so any suggestions are welcome. 我怀疑到目前为止,我的方法有点过高,因此欢迎提出任何建议。

http://rlpetproducts2.designlocker.co.uk/index.php?route=product/product&product_id=251 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);
});

The Goal : on 'Add to Basket' click, check at least one of the selected options is selected (else alert), if one is selected, calculate the total quantity from the inline input fields (where checkbox is selected) (for now simply alert the quantity total). 我们的目标 :“添加到购物篮”点击一下,即可选定选项的至少一个选择(其它警报),如果选择一个,计算从行内输入字段总量 (其中选择复选框)(现在简单提醒数量总计)。

在此处输入图片说明

In this example, the value to be alerted would be 5. 在此示例中,要警告的值为5。

Untested but it should work like this: 未经测试,但它应该像这样工作:

$('#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM