【发布时间】:2018-06-29 02:25:34
【问题描述】:
我有一个如下所示的 jQuery 脚本。假设获得 AWS ML 预测并比较分数,并根据分数附加到表中的单元格中正确的预测。所有这些单元格都具有相同的类prediction。单击按钮并触发此脚本时,它会正确执行所有操作,但是当它到达附加部分时,会抛出错误消息Cannot read property 'append' of undefined。
当我在 chrome 控制台中运行相同的脚本时,它会正确附加到正确的位置。我不确定为什么会这样。
$(document).ready(function(){
var highPred="";
$("#predict").click(function(){
var predictionTables = $(".prediction");
for (var i = 0; i < 4; i++) {
var predictedScoresAWS=[];
var params = {
Record: myData[i]
};
machinelearning.predict(params, function(err, data) {
if (err){
console.log(err, err.stack); // an error occurred
} else{
console.log(data); // successful response
// data = $.parseJSON(data);
predictedScoresAWS.push(data.Prediction.predictedScores['Reduce purge history day count']);
predictedScoresAWS.push(data.Prediction.predictedScores['Increase the java heap space']);
predictedScoresAWS.push(data.Prediction.predictedScores['Successful run']);
predictedScoresAWS.push(data.Prediction.predictedScores['Other error']);
console.log(predictedScoresAWS)
var highPredIndex = predictedScoresAWS.indexOf(Math.max(...predictedScoresAWS))
switch(highPredIndex){
case 0:
highPred='Reduce purge history day count';
break;
case 1:
highPred='Increase the java heap space';
break;
case 2:
highPred='Successful run';
break;
case 3:
highPred='Other error';
}
console.log(highPred);
console.log(predictionTables);
console.log(predictionTables[i])
while (predictedScoresAWS.length) { predictedScoresAWS.pop(); }
predictionTables[i.].append(highPred);
}
});
}
});
});
【问题讨论】:
标签: javascript jquery html web-applications