【问题标题】:Calculate value of text input where checkbox is selected计算选中复选框的文本输入值
【发布时间】:2016-02-21 12:03:42
【问题描述】:

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

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

还有一个问题是,我的复选框在被选中时都没有获得“已选中”的属性,因此我无法定位那些也被选中的复选框。

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

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。

【问题讨论】:

    标签: javascript jquery checkbox


    【解决方案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);
    });
    

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2017-06-12
      • 2017-12-01
      • 1970-01-01
      相关资源
      最近更新 更多