0

我在访问全局变量时遇到问题,想访问

selection1 = $("#slider").slider( "value" );
selection2 = $("#slider2").slider( "value" );

在 jQuery 函数内部只需通过选择调用 calcTmpl(selection1, selection2) ,但这似乎无法通过在函数外部声明变量来实现,如下所示:var selection1 = $("#slider").slider( "value" );

然后滑块消失。

这是滑块执行计算的代码。这很有效,只是我不想一遍又一遍地设置 selection1 和 2。

任何想法为什么这不起作用?

var selection1;
var selection2;
$("#slider").slider({
    range: "min",
    value: 5000000,
    min: 100000,
    max: 10000000,
    slide: function() {
        selection1 = $("#slider").slider("value");
        selection2 = $("#slider2").slider("value");
        calcTmpl(selection1, selection2);
    },
    change: function() {
        selection1 = $("#slider").slider("value");
        selection2 = $("#slider2").slider("value");
        calcTmpl(selection1, selection2);
    }
});

$("#slider2").slider({
    range: "min",
    value: 50000,
    min: 100,
    max: 100000,
    slide: function() {
        selection1 = $("#slider").slider("value");
        selection2 = $("#slider2").slider("value");
        calcTmpl(selection1, selection2);
    },
    change: function() {
        selection1 = $("#slider").slider("value");
        selection2 = $("#slider2").slider("value");
        calcTmpl(selection1, selection2);
    }
});​
calcTmpl(5000000,50000);
4

1 回答 1

2

试试这个:

var selection1 = 5000000,
    selection2 = 50000;

$("#slider").slider({
    range: "min",
    value: selection1,
    min: 100000,
    max: 10000000,
    slide: function(event, ui) {
        selection1 = ui.value;
        calcTmpl(selection1, selection2);
    }
});

$("#slider2").slider({
    range: "min",
    value: selection2,
    min: 100,
    max: 100000,
    slide: function(event, ui) {
        selection2 = ui.value;
        calcTmpl(selection1, selection2);
    }
});
于 2012-11-14T16:31:10.163 回答