javascript - 循环中的函数遇到 "passing"变量问题?

标签 javascript jquery

变量name没有被传递到这些函数中。如何将这些变量传递到这些函数中?我还尝试完全不使用局部变量 name ,而只使用全局变量 arrayOfExpansions ,但也失败了。感谢您的浏览。

var arrayOfExpansions = ["a", "b", "c", "d", "e", "f", "g"];
$(document).ready(

function () {
    for (var int = 0; int < arrayOfExpansions.length; int++) {
        var name = arrayOfExpansions[int].toLowerCase();
        console.log(name + "4");
        $(function (name) {
            console.log(name + "3");
            $("#" + name + "Slider").slider({
                range: true,
                min: 0,
                max: 10,
                values: [0, 10],
                slide: function (event, ui, name) {
                    console.log(name + "2");
                    $("#" + name + "SliderValue").html(ui.values[0] + " - " + ui.values[1]);
                }
            });
            console.log(name + "1");
            $("#" + name + "SliderValue").html($("#" + name + "Slider").slider("values", 0) + " - " + $("#" + name + "Slider").slider("values", 1));
        });
    }
});

我的 console.log() 的输出如下所示:

a4
CardClass.js: 98b4
CardClass.js: 98c4
CardClass.js: 98d4
CardClass.js: 98e4
CardClass.js: 98f4
CardClass.js: 98g4
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1
CardClass.js: 100function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
3
CardClass.js: 111function(selector, context) {

    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init(selector, context, rootjQuery);

}
1

最佳答案

外部“就绪”处理程序中间的位置,其中有:

$(function(name) {

这就是建立另一个“就绪”处理程序。这就是“$(someFunction)”的作用;换句话说,它与“$(document).ready(someFunction)”完全相同。

因此,这根本不会执行您期望的任何操作,并且与参数传递没有太大关系。

如果您希望使用某个参数调用函数,请声明一个函数:

    function theDaleFunction(name) {
        console.log(name + "3");
        $("#" + name + "Slider").slider({
            range: true,
            min: 0,
            max: 10,
            values: [0, 10],
            slide: function (event, ui, name) {
                console.log(name + "2");
                $("#" + name + "SliderValue").html(ui.values[0] + " - " + ui.values[1]);
            }
        });
        console.log(name + "1");
        $("#" + name + "SliderValue").html($("#" + name + "Slider").slider("values", 0) + " - " + $("#" + name + "Slider").slider("values", 1));
    }

将其放在某个地方,也许完全在“for”循环之前,或者可能在“ready”处理程序之外;取决于其他东西。然后在循环中调用它:

      theDaleFunction( name );

关于javascript - 循环中的函数遇到 "passing"变量问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468268/

相关文章:

javascript - 是否有支持 video.js 的 VPAID 插件?

javascript - Jquery - (重新)连接动态生成的元素

javascript - 如何让这个 jQuery 代码在相同的 html 元素上工作?

javascript - Laravel - 将 JSON 响应包装在文本区域中

javascript - 数据表突出显示第一个分页页面之外的行

javascript - 列表框(表格)中的大量数据

javascript - 使用 Angular ng-repeat 在一列中添加多行

javascript - 具有动态内容的 Scrapy 爬行足球统计数据

javascript - 创建一个 jQuery 插件 : best practices regarding functions' visibility?

Javascript 图像翻转