最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - noUiSlider -> use value as form input - Stack Overflow

programmeradmin3浏览0评论

I'm using the noUiSlider and I'm trying to use the slider value as a form input value (to send it in a working form). I've tried various examples that I could find on stack overflow, but nothing seems to work. This is my slider script:

<script>
var slider = $('#slider');
noUiSlider.create(slider[0], {
    start: [2000],
    range: { min: 2000, max: 10000 },
  step: 500,
  tooltips: true,
  connect: [true, false],
});
</script>

Maybe there's a hint in there why the codes I find aren't working. Because to initiate the slider I somewhere found the code var slider = $('#slider'); to work for me and not var slider = document.getElementById('slider'); as shown in most examples and documentation.

I would appreciate a very simple solution/explanation, as I actually don't know javascript at all...

I'm using the noUiSlider and I'm trying to use the slider value as a form input value (to send it in a working form). I've tried various examples that I could find on stack overflow, but nothing seems to work. This is my slider script:

<script>
var slider = $('#slider');
noUiSlider.create(slider[0], {
    start: [2000],
    range: { min: 2000, max: 10000 },
  step: 500,
  tooltips: true,
  connect: [true, false],
});
</script>

Maybe there's a hint in there why the codes I find aren't working. Because to initiate the slider I somewhere found the code var slider = $('#slider'); to work for me and not var slider = document.getElementById('slider'); as shown in most examples and documentation.

I would appreciate a very simple solution/explanation, as I actually don't know javascript at all...

Share Improve this question asked Mar 29, 2018 at 15:26 jakubsudra.jakubsudra. 231 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

noUIslider does not create an html input so I can see 2 easy options.

1- Add a hidden input in your form that will contain the slider value and will be updated each time slider change event is triggered.

<form>
 <div id="slider"></div>
 <input id="sliderValueInput" type="hidden" value=""> 
</form>

<script>
var slider = noUiSlider.create($("#slider")[0], {
    start: [2000],
    range: { min: 2000, max: 10000 },
  step: 500,
  tooltips: true,
  connect: [true, false],
});

//define initial hidden input value with slider value
$("#sliderValueInput").val(slider.get());

//update hidden input value on slider change
slider.on("change", function() {
    $("#sliderValueInput").val(slider.get());
});
</script>

https://codepen.io/anon/pen/mxXYBK

2- Handle your form post data manually at submit with an AJAX request for instance. Demo can be provided if needed...

发布评论

评论列表(0)

  1. 暂无评论