难以将“传递”变量传递给循环中的函数?

时间:2011-12-11 23:09:32

标签: 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

1 个答案:

答案 0 :(得分:2)

那个地方在你的外部“准备好”处理程序的中间,你有这个:

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