首页 > 解决方案 > ReferenceError: $tableRows 未定义 jQuery

问题描述

我收到上述错误,但似乎找不到问题。我尝试了此处发布的各种解决方案,但仍然无法正常工作。

我对 jQuery 比较陌生。

$(document).ready(function(){
    loadCalc.init();

    var tableRows=$("#loadTable tbody tr");
    $tableRows.each(function(index){
        var $tableRow =jQuery(this);
    })

    $tableRow.find('.hours[]').on('change', function(){

        var hrs=$tableRow.find("[name=hours[]]").val();
        var qty=$tableRow.find("[name=quantity[]]").val();
        var watts=$tableRow.find("[name=acwatts[]]").val();
        var totalWatts=parseInt(hrs,10)*parseFloat(qty)*parseFloat(watts);

    })


    if (!isNaN(totalWatts)){

        $tableRow.find('.total[]').val(totalWatts.toFixed(2));
        var grandTotalWatts =0;
        $(".totalWatts").each(function(){

            var stVal = parseFloat($(this).val());
            grandTotalWatts += isNaN(stVal)? 0 : stVal;


            });
        $('#totalSystem').val(grandTotalWatts.toFixed(2));
    }

});

标签: javascriptjquery

解决方案


即使您可以这样做,也不建议使用 JQuery 以 $ 为前缀命名变量!

在您的代码中,您已经初始化了tableRows变量,但没有使用 $ 作为前缀,但是在添加 $ ($tableRows) 之后使用它!

所以,你有两个选择:

  1. 编辑您的var tableRows = ....tovar tableRows = ...
  2. 替换你$tableRowstableRows

推荐阅读