javascript - jQuery UI Slider - Using default value in function - Stack Overflow

I need some help with jQuery UI slider, I have set the initial value but I can't seem to find a wa

I need some help with jQuery UI slider, I have set the initial value but I can't seem to find a way to display it when the page loads. I also need to be able to call both slider values to a function to do a calculation, then display it to the screen.

You can view my Fiddle test here > /

jQuery

$(function() {
    $( "#slider1").slider({ 
        max: 2500,
        value: 1000,
        slide: function( event, ui ) { $( "#slider-result1" ).html( ui.value ); },
        change: function(event, ui) { calPrice(ui.value); }
    });

    $( "#slider2").slider({ 
        max: 30,
        value: 12,
        slide: function( event, ui ) { $( "#slider-result2" ).html( ui.value ); },
        change: function(event, ui) { calDays(ui.value); }
    });
  });

  function calDays(sliderval){
    var day = sliderval;
    day = day * 100;
    document.getElementById('price2').innerHTML = day;
    var price = document.getElementById('price1').innerHTML;
    price = price * day;
    document.getElementById('price3').innerHTML = price;
  }

  function calPrice(sliderval){
    var price = sliderval;
    price = price * 100;
    document.getElementById('price1').innerHTML = price;
  }

Thanks.

I need some help with jQuery UI slider, I have set the initial value but I can't seem to find a way to display it when the page loads. I also need to be able to call both slider values to a function to do a calculation, then display it to the screen.

You can view my Fiddle test here > http://jsfiddle/cC3Qh/

jQuery

$(function() {
    $( "#slider1").slider({ 
        max: 2500,
        value: 1000,
        slide: function( event, ui ) { $( "#slider-result1" ).html( ui.value ); },
        change: function(event, ui) { calPrice(ui.value); }
    });

    $( "#slider2").slider({ 
        max: 30,
        value: 12,
        slide: function( event, ui ) { $( "#slider-result2" ).html( ui.value ); },
        change: function(event, ui) { calDays(ui.value); }
    });
  });

  function calDays(sliderval){
    var day = sliderval;
    day = day * 100;
    document.getElementById('price2').innerHTML = day;
    var price = document.getElementById('price1').innerHTML;
    price = price * day;
    document.getElementById('price3').innerHTML = price;
  }

  function calPrice(sliderval){
    var price = sliderval;
    price = price * 100;
    document.getElementById('price1').innerHTML = price;
  }

Thanks.

Share Improve this question asked Feb 14, 2013 at 6:53 grandpagohangrandpagohan 411 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Add create event handlers to sliders:

$("#slider1").slider({
    max: 2500,
    value: 1000,
    slide: function (event, ui) {
        $("#slider-result1").html(ui.value);
    },
    change: function (event, ui) {
        calPrice(ui.value);
    },
    create: function (event, ui) {
        $("#slider-result1").html($(this).slider("value"));
    }
});

Add this to your ready function:

// display slider value on load
$("#slider-result1").html($("#slider1").slider("value"));
$("#slider-result2").html($("#slider2").slider("value"));

// perform calculations on load
calPrice($("#slider1").slider("value"));
calDays($("#slider2").slider("value"));

http://jsfiddle/samliew/cC3Qh/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744893821a4599575.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信