简体   繁体   English

如何在滑块上切换复选框?

[英]How to toggle checkbox on slider?

I have a simple JQuery Ui slider that contains hidden checkbox inside. 我有一个简单的JQuery Ui滑块,其中包含隐藏的复选框。 I need to add the ability to toggle slider and thus change checkbox value. 我需要添加切换滑块的功能,从而更改复选框的值。

How can I do this? 我怎样才能做到这一点?

Codepen example. Codepen示例。

HTML HTML

<div style="margin-top: 50px"></div>

<div class="col-md-1">
  <div id="slider">
    <input type="checkbox">
  </div>
</div>

JS JS

  $(function() {
    $( "#slider" ).slider({
      step : 99,
      animate : 'slow'
    });
  });

在此处输入图片说明

You can check and uncheck by moving the slide like this 您可以通过移动幻灯片来检查和取消检查

  $(function() {
    $( "#slider" ).slider({
      min: 0,
      max: 1,
      animate : 'slow',
      slide: function( event, ui ) {
        if(ui.value > 0){
          $("#slider input[type='checkbox']").prop("checked", true);
        } else {
          $("#slider input[type='checkbox']").prop("checked", false);
        }
      }
    });
  });

So you need to exam the value when the slider stops. 因此,您需要在滑块停止时检查该值。 This can be done like so: 可以这样完成:

  $(function() {
    $("#slider").slider({
      step: 99,
      animate: 'slow',
      stop: function(e, ui) {
        if (ui.value > 0) {
          $("#slider input[type='checkbox']").prop("checked", true);
        }
      }
    });
  });

The value will be either 0 or 100. So when the slider is to the right, it's at 100% value or some value. value将为0或100。因此,当滑块向右移动时,其值为100%或某个值。 It will be more than 0, so that's what we check for. 它将大于0,这就是我们要检查的内容。

See more about this event function: http://api.jqueryui.com/slider/#event-stop 查看有关此事件功能的更多信息: http : //api.jqueryui.com/slider/#event-stop

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

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