jQuery UI 多个带有金额的滑块

发布于 2024-12-01 01:48:00 字数 820 浏览 3 评论 0原文

我在一页上设置了一些滑块,但是当您滑动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-12-08 01:48:00

还没有测试过,但我很确定这是在查找“.slider-range”和“.amount”元素时不使用任何类型的范围界定的问题。你这样做的方式,基本上是为每个“.amount”元素 X 次附加“slide”事件的处理程序(其中 X 是页面上“.slider”元素的数量)。

我的修复:

$(".slider").each(function () {
    // $this is a reference to .slider in current iteration of each
    var $this = $(this);
    // find any .slider-range element WITHIN scope of $this
    $(".slider-range", $this).slider({
        range: true,
        min: 0,
        max: 100,
        values: [30, 60],
        slide: function (event, ui) {
            // find any element with class .amount WITHIN scope of $this
            $(".amount", $this).val("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    });
    $(".amount").val("$" + $(".slider-range").slider("values", 0) + " - $" + $(".slider-range").slider("values", 1));
});    

编辑:我忘记在声明 $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:

$(".slider").each(function () {
    // $this is a reference to .slider in current iteration of each
    var $this = $(this);
    // find any .slider-range element WITHIN scope of $this
    $(".slider-range", $this).slider({
        range: true,
        min: 0,
        max: 100,
        values: [30, 60],
        slide: function (event, ui) {
            // find any element with class .amount WITHIN scope of $this
            $(".amount", $this).val("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    });
    $(".amount").val("$" + $(".slider-range").slider("values", 0) + " - $" + $(".slider-range").slider("values", 1));
});    

EDIT: I forgot to put var before declaring $this variable. I've tested it and now it looks alright :)

子栖 2024-12-08 01:48:00

您每次都将 .amount 用作 元素的类。由于类通常不是唯一的,因此它会获取具有该类的所有 元素并更改其值。因此,您应该指示它应该在当前容器内查找具有 amount 类的跨度(使用 .find() 方法。(或者您可以使用每个滑块都有唯一的 id。)

因此,如果您想一遍又一遍地使用相同的 HTML,您可以简单地执行以下操作:

$(document).ready(function() {
    $(".slider").each(function() {
        // $this is a reference to .slider in current iteration of each
        $this = $(this);
        // find any .slider-range element WITHIN scope of $this
        $(".slider-range", $this).slider({
            range: true,
            min: 0,
            max: 100,
            values: [ 30, 60 ],
            slide: function( event, ui ) {
               // find any element with class .amount WITHIN scope of $this
               $(this).parent().find(".amount").html( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ]);
            }
        });
        $(".amount").html( "$" + $(".slider-range").slider("values", 0 ) + " - $" + $(".slider-range").slider("values", 1 ));
    });
});

因此,您还应该更改 HTML 并添加跨度:

<div class="slider">
      <b>Price range:</b>
      <span class="amount">0</span>
      <div class="slider-range"></div> 
</div>

<div class="slider">
      <b>Amount range:</b>
      <span class="amount">0</span>
      <div class="slider-range"></div> 
</div>

请注意,我更改了 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:

$(document).ready(function() {
    $(".slider").each(function() {
        // $this is a reference to .slider in current iteration of each
        $this = $(this);
        // find any .slider-range element WITHIN scope of $this
        $(".slider-range", $this).slider({
            range: true,
            min: 0,
            max: 100,
            values: [ 30, 60 ],
            slide: function( event, ui ) {
               // find any element with class .amount WITHIN scope of $this
               $(this).parent().find(".amount").html( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ]);
            }
        });
        $(".amount").html( "$" + $(".slider-range").slider("values", 0 ) + " - $" + $(".slider-range").slider("values", 1 ));
    });
});

You should thus also change your HTML and add the span:

<div class="slider">
      <b>Price range:</b>
      <span class="amount">0</span>
      <div class="slider-range"></div> 
</div>

<div class="slider">
      <b>Amount range:</b>
      <span class="amount">0</span>
      <div class="slider-range"></div> 
</div>

Note that I changed the val() to html() in your JQuery script in order to fill the span content. You can read more about the html() functionality here.

A full working example of this code can be found here on JSFiddle. This should solve your problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文