gpt4 book ai didi

javascript - Jquery slider 在事件更改时更新值

转载 作者:行者123 更新时间:2023-12-02 19:23:54 24 4
gpt4 key购买 nike

我有以下代码,用于根据 slider 值计算服务的价格。

它运行良好,只是 PriceVal 没有实时更新。仅当用户从 slider 上抬起鼠标单击后,它才会更新。

有谁知道如何让它在 slider 滑动时实时更新?

该页面可以在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));
}
});

最佳答案

您需要绑定(bind)onSlide事件

onSlide

When sliding occurs. You can cancel the action by returning false or calling preventDefault() for the event object. Note that assigning this event listener is performance heavy and may affect the smoothness of the sliding experience.

而不是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. For other events you must use the bind method. This leads to more fluent syntax as shown in the example above.

您可以在 $.rangeinput 调用中执行此操作:

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

或者之后使用 bind .

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

关于javascript - Jquery slider 在事件更改时更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com