0

我之前得到了一些关于这段代码的帮助,但我遇到了另一个问题。如何修改每个函数内的变量并在该范围之外使用它们?
这是我的代码的简单解释:对于每个网页,获取元素的内容并将它们插入到数组中。在此之后,操作数组以创建消息并向浏览器发送警报。

HTML

<div id="insert"></div>

Javascript

$(document).ready(function() {
    userid = 17320;
    courseid = 4745;

    allLessonsArray = script(userid, courseid);

    function script(userid, courseid) {
        var lessonInTopicSectionArray = new Array();
        lessonInTopicSectionArray['Floods, external'] = 376;
        var allLessonsArray = new Array();

        $.each(lessonInTopicSectionArray, function(name, lesson) {
            lastAttempt = 0;
            url = '---------/learn/mod/lesson/report.php?id=' + lesson + '&action=reportdetail&userid=' + userid + '&try=' + lastAttempt;
            var lessonArray = new Array();

            $.get(url, function(data) {
                var page = $(data);                
                $('tr.r1.lastrow td.cell.c0').html(page).each(function(index, content) {
                    score = parseInt(content.substr(content.length - 1));
                    lessonArray[index] = score;
                });

                allLessonsArray[name] = lessonArray;
            });

        });
        return allLessonsArray;
    }

    var message = "";

    $.each(allLessonsArray, function(name, lessonArray) {
        if (!message) {
            message = "<li>Lesson <b>" + name + "</b> scores are:</li>";
        }
        else {
            message = message + "<li>Lesson <b>" + name + "</b> scores are:</li>";
        }

        probability = lessonArray[0];
        humanImpact = lessonArray[1];
        infrastructureImpact = lessonArray[2];
        communityImpact = lessonArray[3];
        training = lessonArray[4];
        preparedness = lessonArray[5];
        message = message + "<ul><li>Probability score: " + probability + "</li><li>Human Impact score: " + humanImpact + "</li><li>Public Health Infrastructure Impact score: " + infrastructureImpact + "</li><li>Community Impact score: " + communityImpact + "</li><li>Training score: " + training + "</li><li>Preparedness score: " + preparedness + "</li></ul>";
        risk = (probability / 3) * ((humanImpact + infrastructureImpact + communityImpact + training + preparedness) / 15);
        message = message + "<p>Risk of <b>" + name + "<b> is" + risk + ".</p>";

    });

    $('#insert').html(message);

});​
4

2 回答 2

0

您需要在范围之外定义该变量。

var myvar = [];
$.each(
// stuff happens to myvar
)
于 2012-08-10T20:42:07.053 回答
0

范围似乎是正确的。

也许您的问题与$.each()不迭代以ArrayaString作为索引的元素有关。

在此处尝试以下操作,您只会得到一个弹出窗口0 -> 1

var list = new Array();
list['Floods, external'] = 376;
list[0] = 1;

$.each(list, function(index, item) {
    alert(index + " -> " + item);
});​

从 jQuery 的$.each() 文档页面:

描述:通用迭代器函数,可用于无缝迭代对象和数组。具有长度属性的数组和类数组对象(例如函数的参数对象)由数字索引迭代,从 0 到 length-1。其他对象通过其命名属性进行迭代。

您可以通过以下方式完成等效操作:

var list = new Array();
list[0] = {name : 'Floods, external', value : 376};

$.each(list, function(index, item) {
    alert(item.name + " -> " + item.value);
});​
于 2012-08-10T21:26:55.307 回答