jQuery UI 多个带有金额的滑块
我在一页上设置了一些滑块,但是当您滑动 1 个滑块时,所有滑块的数量都会发生变化...
我怎样才能使滑块彼此分开?如果不是很麻烦,我如何使用跨度而不是输入字段来显示金额?
JS:
$(".slider").each(function () {
$(".slider-range").slider({
range: true,
min: 0,
max: 100,
values: [30, 60],
slide: function (event, ui) {
$(".amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
}
});
$(".amount").val("$" + $(".slider-range").slider("values", 0) +
" - $" + $(".slider-range").slider("values", 1));
});
HTML:
<div class="slider">
<b>Price range:</b>
<input type="text" class="amount" />
<div class="slider-range"></div>
</div>
I have setting up some sliders on 1 page but when you slide 1 slider the amount of all the sliders change...
How can i make a slider seperate from each other? And if it is not to mutch work how can i use a span instead of a inputfield for showing the amount?
JS:
$(".slider").each(function () {
$(".slider-range").slider({
range: true,
min: 0,
max: 100,
values: [30, 60],
slide: function (event, ui) {
$(".amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
}
});
$(".amount").val("$" + $(".slider-range").slider("values", 0) +
" - $" + $(".slider-range").slider("values", 1));
});
HTML:
<div class="slider">
<b>Price range:</b>
<input type="text" class="amount" />
<div class="slider-range"></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还没有测试过,但我很确定这是在查找“.slider-range”和“.amount”元素时不使用任何类型的范围界定的问题。你这样做的方式,基本上是为每个“.amount”元素 X 次附加“slide”事件的处理程序(其中 X 是页面上“.slider”元素的数量)。
我的修复:
编辑:我忘记在声明 $this 变量之前放置 var 。我已经测试过了,现在看起来没问题:)
Haven't tested it but I'm pretty sure it's an issue of not using any kind of scoping when finding ".slider-range" and ".amount" elements. The way you doing it, you're basically attaching handler for "slide" event for every ".amount" element X times (where X is a number of ".slider" elements on page).
My fix:
EDIT: I forgot to put var before declaring $this variable. I've tested it and now it looks alright :)
您每次都将
.amount
用作元素的类。由于类通常不是唯一的,因此它会获取具有该类的所有
元素并更改其值。因此,您应该指示它应该在当前容器内查找具有 amount 类的跨度(使用
.find()
方法。(或者您可以使用每个滑块都有唯一的 id。)因此,如果您想一遍又一遍地使用相同的 HTML,您可以简单地执行以下操作:
因此,您还应该更改 HTML 并添加跨度:
请注意,我更改了
val()
到在 JQuery 脚本中使用html()
来填充跨度内容,您可以阅读有关html()
功能的更多信息 此处。可以在 在 JSFiddle 上这应该可以解决您的问题。 问题!
You are using
.amount
as a class every time for your<input />
element. Because classes are usually not unique it takes all<input />
elements with that class and changes its value. So you should indicate that it should look for the span with the amount class INSIDE your current container (use the.find()
method. (Or you could make use of an unique id for each slider.)So if you want to use the same HTML over and over you could simply do:
You should thus also change your HTML and add the span:
Note that I changed the
val()
tohtml()
in your JQuery script in order to fill the span content. You can read more about thehtml()
functionality here.A full working example of this code can be found here on JSFiddle. This should solve your problem!