简体   繁体   English

事件更改时Jquery滑块更新值

[英]Jquery slider update value on event change

I have the following code which I'm using to calculate the price of a service based on the sliders value. 我有以下代码,我用它来计算基于滑块值的服务价格。

It works well except the PriceVal doesn't update in realtime. 除了PriceVal没有实时更新外,它运作良好。 It only updates once the user has lifted the mouse click from the slider. 一旦用户从滑块上抬起鼠标,它就会更新。

Does anyone know how I can get it to update in realtime as the slider slides? 当滑块滑动时,有谁知道如何让它实时更新?

The page can be viewed at http://www.voxlogic.net/pricing/ 该页面可在http://www.voxlogic.net/pricing/上查看

$(function() {
//document.getElementById("f1").reset();
$(":range").rangeinput({ progress: true, max: 100, value:1 });  
$(":range").change(function(){
    var sliderVal = this.value;
    calculatePrice(sliderVal);      
});
$(":range").keyup(function(){
    var sliderVal = this.value;
    if (sliderVal==""){
        $("#priceVal").html('');
}
else
        {
            //check if value is from 1..25 range and correct otherwise
            if (isNaN(sliderVal)){sliderVal=1;}
            if (sliderVal<1){sliderVal=1;}
            if (sliderVal>25){sliderVal=25;}
            this.value = sliderVal;
            calculatePrice(sliderVal);
        }
    });

    $(":range").keydown(function(){
        var sliderVal = this.value; 
        calculatePrice(sliderVal);      
});
});

function calculatePrice(sliderVal){
    if (isNaN(sliderVal)){sliderVal=1;}
    if (sliderVal<1){sliderVal=1;}
    if (sliderVal>25){sliderVal=25;}
    var priceVal;
    if (sliderVal<=9){
        priceVal = sliderVal*6;
    }
    else if ((sliderVal>9)&&(sliderVal<=19)){
        priceVal = 9 * 6 + (sliderVal-9) * 4.5;         
    }
    else if (sliderVal>19){
        priceVal = 9 * 6 + 10 * 4.5 + (sliderVal-19) * 3.6;
    }
    $("#priceVal").html('&pound;'+priceVal.toFixed(2));
}
});

You need to bind the onSlide event 您需要绑定onSlide事件

onSlide

When sliding occurs . 滑动时发生 You can cancel the action by returning false or calling preventDefault() for the event object. 您可以通过返回false或为事件对象调用preventDefault()来取消操作。 Note that assigning this event listener is performance heavy and may affect the smoothness of the sliding experience. 请注意,分配此事件侦听器的性能很重,可能会影响滑动体验的平滑性。

instead of the change event. 而不是change事件。

change

After the range value is changed . 范围值更改后 If you slide the range by clicking on the underlying slider this is the only event that is being triggered. 如果通过单击基础滑块来滑动范围,则这是唯一被触发的事件。 Note that the name is the same as the jQuery's change method and you can bind the event directly with that function. 请注意,该名称与jQuery的更改方法相同,您可以直接将该事件与该函数绑定。 For other events you must use the bind method. 对于其他事件,您必须使用bind方法。 This leads to more fluent syntax as shown in the example above. 这导致更流畅的语法,如上例所示。

You can do this in your $.rangeinput call: 您可以在$.rangeinput调用中执行此$.rangeinput

$(":range").rangeinput({
  progress: true,
  max: 100,
  value: 1,
  onSlide: function(evt, val) {
    calculatePrice(val);
  }
});  

Or afterwards using bind . 或者之后使用bind

$(":range").bind('onSlide', function(evt, val){
  calculatePrice(val);
});

You need to hookup to the slide event: 您需要连接到幻灯片事件:

$(".SliderDiv").slider({ min: 0, max: 16, step: .25, slide: SliderSlide, animate: true });

// ---- SliderSlide ---------------------------
//
// user is dragging or clicking a slider

function SliderSlide(event, ui)
{
    var Value = ui.value
    var $Sender = $(ui.handle);
    var $Row = $Sender.parentsUntil("table", "tr");
    ...
    // Call your function here

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

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