在.each()范围内使用变量

时间:2012-08-10 20:36:07

标签: jquery

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

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);

});​

2 个答案:

答案 0 :(得分:0)

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

var myvar = [];
$.each(
// stuff happens to myvar
)

答案 1 :(得分:0)

范围似乎是正确的。

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

尝试以下here,您只会弹出0 -> 1

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

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

来自jQuery&#39; $.each() documentation页面:

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

您可以使用以下内容完成等效操作:

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

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