如果已设置,如何在加载时设置 jquery 滑块的值
我有一个 jquery 滑块和一个存储值的文本框。我正在运行一个 asp.net 应用程序,并且想要加载带有该值的文本框(如果已经存储了该值)并且让滑块反映该值。我认为 jquery 中的一个简单的 if 语句就可以做到这一点,但我不熟悉这个函数。
这是我当前的 jquery:
$("#cultureSlider").slider({
value: 50,
min: 1,
max: 100,
step: 1,
slide: function (event, ui) {
$(".cultureScore").val("" + ui.value);
}
});
我认为这样的东西会起作用(但事实并非如此):
$("#cultureSlider").slider({
if($(".cultureScore").val() == 50){
value: 50,
} else {
value: $(".cultureScore").val(),
}
min: 1,
max: 100,
step: 1,
slide: function (event, ui) {
$(".cultureScore").val("" + ui.value);
}
});
I have a jquery slider and a textbox that stores the value. I'm running an asp.net application and want to load the textbox with the value if one is already stored and have the slider reflect the value. I'm thinking that a simple if statement in the jquery would do it, but i'm unfamiliar with the function.
Here is my current jquery:
$("#cultureSlider").slider({
value: 50,
min: 1,
max: 100,
step: 1,
slide: function (event, ui) {
$(".cultureScore").val("" + ui.value);
}
});
I would think something like this would work (but it doesn't):
$("#cultureSlider").slider({
if($(".cultureScore").val() == 50){
value: 50,
} else {
value: $(".cultureScore").val(),
}
min: 1,
max: 100,
step: 1,
slide: function (event, ui) {
$(".cultureScore").val("" + ui.value);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我很确定
if
语句在您所得到的地方是无效的,并且我认为您的检查无论如何都是多余的,无论if
结果是否为TRUE< /code> 或
FALSE
你总是会使用$(".cultureScore").val()
:I'm pretty sure an
if
statement is invalid where you've got it, and I think your check is redundant anyway, whether theif
results inTRUE
orFALSE
you're always going to use$(".cultureScore").val()
:我认为你的语法是错误的......尝试内联 if (忘记了技术术语)或在滑块实例化之外执行计算。
方法一
方法二
I think your syntax is wrong... try either an inlined if (forgot the technical term) or performing the calculation outside the slider instantiation.
Method one
Method two
为什么不这样做:
Why not do this: