简体   繁体   English

多个框的jQuery滑块

[英]jQuery slider for multiple boxes

I have a Asp.Net MVC create method in which a user must fill out how much time they are associating to a plan. 我有一个Asp.Net MVC创建方法,其中用户必须填写他们与计划相关联的时间。 I have the following jquery slider which populates one box. 我有下面的jQuery滑块填充一个框。 I now need the slider to do the same for all days of the week hours and minutes. 现在,我需要滑块在一周中的所有小时和分钟内执行相同的操作。

Currently I have 目前我有

 $("#slider-range-max").click(function () {
        $("#MondayMinutes").slider({
            range: "max",
            min: 0,
            max: 59,
            value: 15,
            slide: function (event, ui) {
                $("#MondayMinutes").val(ui.value);
            }
        });
        $("#MondayMinutes").val($("#slider-range-max").slider("value"));
    });

    $("#MondayHours").click(function () {
        $("#slider-range-max").slider({
            range: "max",
            min: 1,
            max: 12,
            value: 2,
            slide: function (event, ui) {
                $("#MondayHours").val(ui.value);
            }
        });
        $("#MondayHours").val($("#slider-range-max").slider("value"));
    });

But would I have to do that for each day of the week or is there a more efficient way of doing this? 但是我是否必须在一周的每一天都这样做,还是有更有效的方法呢?

Many thanks. 非常感谢。

Here's how you can use the same slider for multiple inputs: 这是将相同的滑块用于多个输入的方法:

 $(function () { // Init slider $("#slider-range-max").slider({ range: "max" }); // Show it on an input click $('.my-slider-input').on('click', function(){ var $this_input=$(this); var $slider_node=$('#slider-range-max'); // Set input-specific slider options $slider_node.slider('option', { min: $this_input.data('min'), max: $this_input.data('max'), value: $this_input.val(), slide: function (event, ui) { $this_input.val(ui.value); } }); // Show the slider in place $slider_node.insertAfter($this_input).show(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css"> <div> <label for="mon-h">Mon hours:</label> <input type="text" id="mon-h" class="my-slider-input" data-min="1" data-max="12" value="2" /> </div> <div> <label for="mon-m">Mon minutes:</label> <input type="text" id="mon-m" class="my-slider-input" data-min="0" data-max="59" value="15" /> </div> <div> <label for="tue-h">Tue hours:</label> <input type="text" id="tue-h" class="my-slider-input" data-min="1" data-max="12" value="2" /> </div> <div> <label for="tue-m">Tue minutes:</label> <input type="text" id="tue-m" class="my-slider-input" data-min="0" data-max="59" value="15" /> </div> <div id="slider-range-max" style="display: none;"></div> 

Note that slider options are stored in the attributes of input elements ( data-min , data-max , value ) 请注意,滑块选项存储在input元素的属性( data-mindata-maxvalue )中

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

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