【问题标题】:JQuery Slider - How to also change the slider input value changeJQuery Slider - 如何更改滑块输入值的变化
【发布时间】:2018-12-03 22:47:10
【问题描述】:

除了在更改滑块时更改输入值之外...我如何反之亦然在输入更改时更改滑块?

html:

<div style="margin-bottom:20px;">
    <label for="amount">Price range:</label>
    <span style="float:right;"> 
        <input id="amount-min" type="number" placeholder="0.000" style="border: 1px solid #ddd;; color:#f6931f; font-weight:bold; text-align:center; width:75px; padding-bottom: 0px; padding-top: 0px;">
        <input id="amount-max" type="number" placeholder="0.000" style="border: 1px solid #ddd;; color:#f6931f; font-weight:bold; text-align:center; width:75px; padding-bottom: 0px; padding-top: 0px;">
    </span>
</div>
<div id="slider-range"></div>

javascript:

$(function() {
    $( "#slider-range" ).slider({
        range: true,
        min: 0,
        max: 500,
        values: [ 75, 300 ],
        slide: function( event, ui ) {

            $( "#amount-min" ).val( ui.values[ 0 ] );
            $( "#amount-max" ).val( ui.values[ 1 ] );
        }
    });

    $( "#amount-min" ).val( $( "#slider-range" ).slider( "values", 0 ) );
    $( "#amount-max" ).val( $( "#slider-range" ).slider( "values", 1 ) );

    $('#amount-min').on('change', function () {
        $(this).val( $( "#slider-range" ).slider( "values", 0 ) );
    });        

    $('#amount-max').on('change', function () {
        $(this).val( $( "#slider-range" ).slider( "values", 1 ) );
    });         

});

小提琴:

http://jsfiddle.net/6e4gLmc2/

我想保持给定的原始范围。而且我只想允许输入值在范围内并考虑其他值

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-slider


    【解决方案1】:

    我建议您将on change 更改为on input。在type number 输入上效果更好,因为您希望值在用户更改输入中的值时立即更改(包括使用箭头或向上/向下键)

    此事件类似于 onchange 事件。不同之处在于,oninput 事件在元素的值发生更改后立即发生,而 onchange 事件发生在元素失去焦点、内容发生更改后。

    然后只需添加 this.val() (即输入 val )作为 slider 的低值或高值

    $(function() {
         $("#slider-range").slider({
           range: true,
           min: 0,
           max: 500,
           step: 0.001,
           values: [35.113, 300.123],
           slide: function(event, ui) {
    
             $("#amount-min").val(ui.values[0]);
             $("#amount-max").val(ui.values[1]);
           }
         });
         
         $("#amount-min").val($("#slider-range").slider("values", 0));
         $("#amount-max").val($("#slider-range").slider("values", 1));
         console.log(
             $("#amount-min").val() );
         $('#amount-min').on('input', function() {
           if ($(this).val() < $("#amount-max").val()) {
             $("#slider-range").slider('values', 0, $(this).val());
           }
    
         });
    
         $('#amount-max').on('input', function() {
           if ($(this).val() > $("#amount-min").val()) {
             $("#slider-range").slider('values', 1, $(this).val());
           }
         });
    
    
       });
    <title>jQuery UI Slider - Range slider</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    
    
    
    <div style="margin-bottom:20px;">
      <label for="amount">Price range:</label>
      <span style="float:right;">	
    						<input id="amount-min" step=".001" type="number" placeholder="0.000" style="border: 1px solid #ddd;; color:#f6931f; font-weight:bold; text-align:center; width:75px; padding-bottom: 0px; padding-top: 0px;">
    						<input id="amount-max" step=".001" type="number" placeholder="0.000" style="border: 1px solid #ddd;; color:#f6931f; font-weight:bold; text-align:center; width:75px; padding-bottom: 0px; padding-top: 0px;">
    					</span>
    </div>
    <div id="slider-range"></div>

    【讨论】:

    • 非常感谢。有没有办法另外只允许小于当前最大值的最小值......反之亦然?
    • @PhilippM 是的,这很简单。只需将this val 与另一个输入val 进行比较。请参阅编辑后的答案。您可以将else 添加到该if 并在出现错误时执行任何您想要的操作。显示类似的消息或消息
    • 非常感谢。最后一个愿望:-) ...我怎样才能把它全部放在小数点后 3 位? ...所以最小 0.000 到最大 500.000 和值:[75.000, 300.000] ...如果我改变它,它不会让我喜欢这个
    • @PhilippM 将step=".001" 添加到滑块和输入中。查看编辑后的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    相关资源
    最近更新 更多