简体   繁体   English

使用jQuery创建具有不同类名或ID的多个元素

[英]Creating multiple elements with different class names or IDs using jQuery

I'm trying to create multiple sliders with different class names or IDs using http://refreshless.com/nouislider/ so that their individual value can be used by backend functionality. 我正在尝试使用http://refreshless.com/nouislider/创建具有不同类名或ID的多个滑块,以便后端功能可以使用它们的单独值。 I could write a new function with different inputs for each slider but that would be repeating myself. 我可以为每个滑块编写一个具有不同输入的新函数,但这会重复我自己。 I think this might be an appropriate situation for $(this) but I'm not too sure how to use it. 我认为这对于$(this)可能是合适的情况,但是我不太确定如何使用它。 If it could be done in handlebars.js that would make my day. 如果可以在handlebars.js中完成,那将是我的一天。

Noob question I know, but I can seem to find an appropriate answer or tutorial through my searches, which is probably due to not knowing the appropriate terminology. 我知道了Noob问题,但似乎可以通过搜索找到合适的答案或教程,这可能是由于不知道合适的术语所致。

Here's my create slider function: 这是我的创建滑块功能:

this.prefSlider = function () {

    $(".prefSlider").noUiSlider({
        start: [50],
        range: {
            'min': 0,
            'max': 100
        }
    });
    $('.prefSlider').Link('lower').to($('.prefLower'), 'text', wNumb ({
        decimals: 0
    }));
}

And here's the html rendering it: 这是呈现它的html:

<div class="sliderBox">
                                <div class="prefSlider">
                                </div>
                                <div class="sliderValues">
                                    <span class="prefLower"></span>
                                </div>
                            </div>

Thank you 谢谢

You can create multiple elements and just call them by their id and not the class 您可以创建多个元素,仅通过其ID(而不是类)来调用它们

$("#slider1").noUiSlider({
  start: [50],
  range: {
    'min': 0,
    'max': 100
  }
});

$("#slider2").noUiSlider({
  start: [50],
  range: {
    'min': 10,
    'max': 50
  }
});

<div id="slider1" class="sliderBox">
  <div class="prefSlider"></div>
  <div class="sliderValues">
    <span class="prefLower"></span>
  </div>
</div>

<div id="slider2" class="sliderBox">
  <div class="prefSlider"></div>
  <div class="sliderValues">
    <span class="prefLower"></span>
  </div>
</div>

how's this for a handlebars template? 车把模板怎么样?

        <div class="sliderBox">
            <div class="prefSlider" data-slider="one-handle-{{this.name}}"></div>
            <div class="sliderValues">
                <span class="prefLower"></span>
            </div>
        </div>

then the js would use the slider name to identify it to the backend: 然后js将使用滑块名称将其标识到后端:

$('[data-slider]').on('change', function (event) {
    var element = this;//could use `event.target` instead of `this`.
    var $element = $(element);
    var name = $element.data('slider');
    var sliderIndex = $('[data-slider="' + name + '"]').index(element);//in case multiple sliders have the same name

    // use name, sliderIndex, and $element.val() as you wish
});

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

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