Replace hidden field value with value of NoUiSlide

2019-06-22 07:35发布

I have used the NoUiSlider JS plugin on my website which allows the user to use a slider to select a budget, which is great but I want the value they choose to be included as part of a contact/quote form.

I have added a hidden field to the form which I assume the value needs to replace from the NoUiSlider part.

Here is my code, any ideas?

<div id="slider-range"></div>

<div id="budget_value">
    &pound;<div id="slider-range-value"></div>
</div>

<input type="text" name="budget" value="" id="budget" class="hid"/>

<script>
    var rangeSlider = document.getElementById('slider-range');
    noUiSlider.create(rangeSlider, {
        start: [ 1500 ],
        step: 250,
        range: {
            'min': [  0 ],
            'max': [ 10000 ]
        }
    });
    var rangeSliderValueElement = document.getElementById('slider-range-value');
    rangeSlider.noUiSlider.on('update', function( values, handle ) {
        rangeSliderValueElement.innerHTML = values[handle];
    });
</script>

Kind regards Liam

1条回答
别忘想泡老子
2楼-- · 2019-06-22 07:48

First of all there is a hidden input type:

<input type="hidden" name="budget" value="" id="budget-input" />

Then you just set the value as follows:

var budgetInput = document.getElementById('budget-input');

rangeSlider.noUiSlider.on('update', function( values, handle ) {
    rangeSliderValueElement.innerHTML = values[handle];
    budgetInput.value = values[handle];
});
查看更多
登录 后发表回答