Jquery滑块全局javascript变量

时间:2012-11-14 16:11:41

标签: jquery variables slider global

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

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

1 个答案:

答案 0 :(得分: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);
    }
});