dqqfuth6736 2014-04-27 10:15
浏览 302
已采纳

noUiSlider在页面加载时设置值

I'm using the noUiSlider on a new project of mine.

I have the element within a form that allows the user to select a range of price (min and max). When the form is submitted both values are used in the code to filter items of a range between these two selected prices.

HTML/PHP:

<div id="m-price"></div>

<div class="field field-range">
    <input type="text" min="0" max="5000" step="100" id="price-min" name="price-min" value="<?php echo ($_POST['price-min']) ? $_POST['price-min'] : ''; ?>">
</div>

<div class="field field-range">
    <input type="text" min="0" max="5000" step="100" id="price-max" name="price-max" value="<?php echo ($_POST['price-max']) ? $_POST['price-max'] : ''; ?>">
</div>

JS:

$('#m-price').noUiSlider({
    range: {
        'min': 0,
        'max': 500
    }
    ,step: 100
    ,start: [0,500]
    ,connect: true
    ,serialization: {
        lower: [
            $.Link({
                target: $('#price-min'),
                format: {
                    decimals: 3,
                    mark: ','
                }
            })
        ],
        upper: [
            $.Link({
                target: $('#price-max'),
                format: {
                    decimals: 3,
                    mark: ','
                }
            })
        ]
    }
});

Now this is currently working perfectly, however, I have on the form elements an 'if value exists set that value to the form input(s) value' every time the page reloads (just like a sticky form thing so the user knows what they searched for).

The way the noUiSlider works this is not possible as the element initializes on page load and the values are set using 'start: [0,500]'. I notice there is a 'set' event but how can I use it in this scenario?

  • 写回答

1条回答 默认 最新

  • drccfl9407 2014-04-27 15:29
    关注

    This could be solved by setting the value to the slider, instead of to the inputs. Basically, you would get your variable from the POST, and get that to javascript:

    <?php 
        $min = $_POST['price-min'];
        $max = $_POST['price-max'];
    ?>
    
    <script>var startValues = [<?php
        echo isset($min) ? $min : 0;
    ?>, <?php
        echo isset($max) ? $max : 500;
    ?>];</script>
    

    Now, you have a value to initiate your slider with:

    $('#m-price').noUiSlider({
        ...
        start: startValues 
        ...
    });
    

    The Link elements will then write these values to the inputs, or, if they weren't set, default to your initial start values of 0 and 500.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况