简体   繁体   English

jQuery UI滑块更改“最大”值

[英]jQuery UI-slider changing “max” value

I'm using the jQuery UI slider for a casino game project I am working on. 我正在将jQuery UI滑块用于我正在从事的赌场游戏项目。 I need to be able to change the "max" value based upon the players current bankroll. 我需要能够根据玩家当前的资金来更改“最大”值。 At first, the slider works correctly, it sets the "max" slider value to 1000, the players starting bankroll. 首先,滑块正常工作,将“最大”滑块值设置为1000,玩家开始提供资金。 Bust as the 'currentBankroll' variable changes, the sliders "max" value does not update. 随着'currentBankroll'变量的更改而崩溃,滑块的“ max”值不会更新。 I inserted a console.log() line of code to check the value of the variable 'currentBankroll', which confirms the value is changing, but the slider "max" value stays at the initial value of 1000 and does not update. 我插入了console.log()代码行,以检查变量'currentBankroll'的值,该值确认该值正在更改,但是滑块“ max”值保持在初始值1000,并且不会更新。 To further confuse me, I tried a solution I found here to change the "max" value. 为了进一步混淆我,我尝试了一种在此处找到的解决方案来更改“最大”值。

$('#slider-range-max2').slider("option", "max", 300);

This line of code will change the sliders "max" value if I put a number in it (like 300), but will not work if I use a variable (like 'currentBankroll"). How can I change the sliders value using a variable? 如果我在其中输入数字(例如300),则此行代码将更改滑块的“ max”值,但如果使用变量(如“ currentBankroll”),则该代码将不起作用。如何使用变量更改滑块的值?

$(function() {
    console.log("Current Bankroll: " + currentBankroll);
    $("#slider-range-max2").slider({
        range: "max",
        max: 0,
        min: 1,
        max: currentBankroll,
        value: 2,
        slide: function( event, ui ) {
            $("#amount").val( ui.value );
            sliderValue = (ui.value);
        },
        slide : function(event, ui) {
            skillLevelSlots(ui.value);
            wagerAmount = (ui.value);
            $('.wager').html("$" + wagerAmount);
        }
    });
    $('#slider-range-max2').slider("option", "max", 300);
});

Right - you are defining your slider at a certain time and using currentBankroll to set max value. 正确-您正在某个时间定义滑块,并使用currentBankroll设置最大值。 This is the value when the slider is created but this doesnt change unless you re create it. 这是创建滑块时的值,但是除非重新创建,否则它不会改变。 To get it to change, whenever your currentBankroll changes you could call a function that changes the value. 要更改它,每当currentBankroll更改时,您都可以调用一个更改值的函数。

function currentBankrollChange(currentBankroll){
     $('#slider-range-max2').slider("option", "max", currentBankroll);
}

or when a players bankroll changes you could call: 或当玩家资金变化时,您可以致电:

 $('#slider-range-max2').slider("option", "max", currentBankroll);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM