得到NaN错误 - Javascript

时间:2016-08-25 03:33:05

标签: javascript ajax

我在总成绩中遇到NaN错误。有人可以帮我找到这个错误的原因吗?或者给我一些想法如何解决这个问题?我只是javascript和web开发的新手。对不起我的代码的逻辑..

enter image description here

这是我的代码。

              function Calculate(){
                var term = $("#term").val();
                var fac_code = $("#faculty_code").val();
            $.ajax({    
              type: 'POST',                                  
              url: 'getrecords.php',                        
              data: {
                "done": 1,
                "term": term,
                "fac_code": fac_code

              },                       
              dataType: 'json',                  
              success: function(data)         
              {
                var major = data[3];              
                var quizzes = data[4];    
                var homework = data[5];      
                var attendance = data[6]; 
                var laboratory = data[7];  
                var activity = data[8];  
                var recitation = data[9];


              var q = +(document.getElementById('quiz').textContent);
              var a = +(document.getElementById('atten').textContent);
              var h = +(document.getElementById('home').textContent);
              var r = +(document.getElementById('reci').textContent);
              var m = +(document.getElementById('me').textContent);
              var ac = +(document.getElementById('activityy').textContent);
              var l = +(document.getElementById('laboratory').textContent);

              var ma = $('.me');
              var qui = $('.quize');
              var homesea = $('.hos');
              var re = $('.recits');
              var laborat = $('.labo');
              var activit = $('.activity');
              var attenda = $('.atte');

              var MisVisible = ma.is(':visible');
              var QisVisible = qui.is(':visible');
              var HisVisible = homesea.is(':visible');
              var RisVisible = re.is(':visible');
              var LisVisible = laborat.is(':visible');
              var AisVisible = activit.is(':visible');
              var ATisVisible = attenda.is(':visible');

                var mt;
                var qt;
                var ht;
                var rt;
                var act;
                var lt;
                var att;

                if (MisVisible === true) {

                  mt = m / 100 * 50 + 50;

                } 
                if (QisVisible === true){

                 qt = q / 100 * 50 + 50;

                }
                if(HisVisible === true){

                 ht = h / 100 * 50 + 50;

                }
                if(RisVisible === true){

                  rt = r / 100 * 50 + 50;
                }
                if(LisVisible === true){

                  lt = l / 100 * 50 + 50;   
                }
                if(AisVisible === true){

                  act = ac / 100 * 50 + 50;

                }
                if(ATisVisible === true){

                    att = a / 100 * 50 + 50;
                }




                  mtt = mt * (major * 0.01);
                  qtt = qt * (quizzes * 0.01);
                  attt = att * (attendance * 0.01);
                  htt = ht * (homework * 0.01);
                  rtt = rt * (recitation * 0.01);
                  actt = act * (activity * 0.01);
                  ltt = lt * (laboratory * 0.01);

          var grade = mtt + qtt + attt + htt + rtt + actt + ltt;

          document.getElementById('td_grade').innerHTML = grade;




              } 
            });
        }

2 个答案:

答案 0 :(得分:2)

最佳答案是了解如何使用Dev Tools源窗格。在代码中放置断点,然后开始单步执行代码以查看值。

如果我采取疯狂的猜测,其中一个是undefined或遗失。

var major = data[3];              
var quizzes = data[4];    
var homework = data[5];      
var attendance = data[6]; 
var laboratory = data[7];  
var activity = data[8];  
var recitation = data[9];

或者其中一个无法由一元+正确解析为数字

var q = +(document.getElementById('quiz').textContent);
var a = +(document.getElementById('atten').textContent);
var h = +(document.getElementById('home').textContent);
var r = +(document.getElementById('reci').textContent);
var m = +(document.getElementById('me').textContent);
var ac = +(document.getElementById('activityy').textContent);
var l = +(document.getElementById('laboratory').textContent);

答案 1 :(得分:1)

按F12键使用Chrome的开发工具,然后转到控制台标签,检查您可以使用的任何变量:

console.log(variable_name);

此外,您的HTML值可能不是int形式,因此您不应该使用一元加号来获取值,而是使用它来检查您的值是否正常:

if (!isNaN(parseFloat("10000")){
    //do something
}