Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当选中了同一行上的复选框时,jquery/javascript才能求和字段_Javascript_Jquery - Fatal编程技术网

仅当选中了同一行上的复选框时,jquery/javascript才能求和字段

仅当选中了同一行上的复选框时,jquery/javascript才能求和字段,javascript,jquery,Javascript,Jquery,我有一个jquery/javascript函数,用于计算我的订单中的多维数据集的总数。这是100%的工作,低于 function calculateTotalVolume() { var grandTotalCubes = 0; $("table.authors-list").find('input[name^="cubicvolume"]').each(function () { grandTotalCubes += +$(this).val(); })

我有一个jquery/javascript函数,用于计算我的订单中的多维数据集的总数。这是100%的工作,低于

function calculateTotalVolume() {
    var grandTotalCubes = 0;
    $("table.authors-list").find('input[name^="cubicvolume"]').each(function () {
        grandTotalCubes += +$(this).val();
    });
    $("#grandtotalcubes").text(grandTotalCubes.toFixed(2));
}
如前所述,上述方法非常有效。我需要第二个函数来合计同一字段,但前提是选中了名为treated的复选框。每行都有一个名为treated的复选框,但由于表是动态生成的,因此每次都会在名称后面追加一个计数器,因此我使用了
name^=“treated”

我想要下面这样的东西,但这不起作用:

function calculateTotalTreatedVolume() {
    var grandTotaltreatedCubes = 0;
    $("table.authors-list").find('input[name^="cubicvolume"]').each(function () {
        if($("table.authors-list").find('checkbox[name^="treated"]').checked){
            alert('10');
            grandTotaltreatedCubes += +$(this).val();
    }

    });
    $("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
}
一如既往地感谢你的帮助

更新

呈现的HTML输出[1添加了动态行]:(仍在开发中,非常粗糙,请原谅)


ProductPrice/CubeQtyline总成本折扣每捆立方体每捆处理的体积
总计:R
平均折扣:%
立方体总数:
经处理的立方体:

首先转到父tr,然后使用“查找”来查找当前行中的复选框,还可以使用“与DOM对象而非jQuery对象一起检查”,您可以使用indexer将jQuery对象转换为DOM对象

改变

if($("table.authors-list").find('checkbox[name^="treated"]').checked){


checked
是实际DOM元素的属性,您拥有的是一个jQuery元素。您需要更改以下内容:

$("table.authors-list").find('checkbox[name^="treated"]').checked
为此:

$("table.authors-list").find('checkbox[name^="treated"]')[0].checked
                                                         -^- // get DOM element
或更多jQuery-ish:

$("table.authors-list").find('checkbox[name^="treated"]').is(':checked')

更改以下行

if($("table.authors-list").find('input[name^="treated"]').checked){
对此

if($("table.authors-list").find('input[name^="treated"]').is(':checked')){

您可以使用
$(“table.authors list”).find('checkbox[name^=“treated”]:checked')
遍历“checked”复选框,并使用最靠近它的输入值(假定位于同一行中)

假设您的表有许多行,每行都有一个复选框和一个输入,您可以使用:

function calculateTotalTreatedVolume() {
    var grandTotaltreatedCubes = 0;

    // iterate through the "checked" checkboxes
    $("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {

        // use the value of the input in the same row
        grandTotaltreatedCubes += +$(this).closest('tr').find('input[name^="cubicvolume"]').val();

    });
    $("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
}
试试这个:

var grandTotaltreatedCubes = 0;

// Cache the table object here for faster processing of your code..
var $table = $("table.authors-list");

$table.find('input[name^="cubicvolume"]').each(function () {

    // Check if checkbox is checked or not here using is(':checked')
    if ($table.find('checkbox[name^="treated"]').is(':checked')) {
        grandTotaltreatedCubes += $(this).val();
    }
});

$("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));

谢谢Adil,我理解您正在尝试做什么,但现在输出一个错误
TypeError:$(…).closest(…).find(…)[0]未定义
…$(“table.authors list”).closest('tr').find('checkbox[name^=“treated”]')[0]。check…
谢谢标记,进行了更改,但未触发警报,因此未捕获事件?谢谢techfoobar,尝试了您的建议,但它没有填充
grandtotaltreatedcubes
span,因此不确定是否100%正确?您也可以发布您的html结构吗?谢谢,我已将此添加到问题中。编辑了答案。复选框的选择器不是
checkbox
,而是
input[type=“checkbox”]
,它将是
find('input[type=“checkbox”][name^=“treated”]')。不是(':checked')
谢谢,出现错误:
$table.find('checkbox[name^=“treated”]')。是(':checked')){
谢谢Will,尝试了一下,但没有工作。没有错误或输出。if语句没有捕获任何内容?令人惊讶的是,所有回答者(包括我)都没有发现
中无效的
复选框。
.find()
!!可能给了我太多的信任:-)谢谢Elclans,没有运气就尝试了。错误出现在[0]?
function calculateTotalTreatedVolume() {
    var grandTotaltreatedCubes = 0;

    // iterate through the "checked" checkboxes
    $("table.authors-list").find('input[type="checkbox"][name^="treated"]:checked').each(function () {

        // use the value of the input in the same row
        grandTotaltreatedCubes += +$(this).closest('tr').find('input[name^="cubicvolume"]').val();

    });
    $("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));
}
var grandTotaltreatedCubes = 0;

// Cache the table object here for faster processing of your code..
var $table = $("table.authors-list");

$table.find('input[name^="cubicvolume"]').each(function () {

    // Check if checkbox is checked or not here using is(':checked')
    if ($table.find('checkbox[name^="treated"]').is(':checked')) {
        grandTotaltreatedCubes += $(this).val();
    }
});

$("#grandtotaltreatedcubes").text(grandTotaltreatedCubes.toFixed(2));