简体   繁体   English

动态启用和禁用jquery旋钮

[英]Enable and disable jquery knob dynamically

I'm using the excellent jQuery knob plugin . 我正在使用出色的jQuery旋钮插件 However, I need to dynamically enable/disable the element depending on user input. 但是,我需要根据用户输入动态地启用/禁用该元素。 There is support for having a disabled state on page load which have the effect that no mouse (or touch) events are bound to the canvas element. 支持在页面加载时设置为disabled状态,从而不会将任何鼠标(或触摸)事件绑定到canvas元素。 Does anyone know how to resolve this issue, that is, how to (after page load) bind and unbind these mouse event listeners? 有谁知道如何解决此问题,也就是说,如何(在页面加载后)绑定和取消绑定这些鼠标事件侦听器?

Ideally I would like to do something like this (on a disabled knob) 理想情况下,我想做这样的事情(在禁用的旋钮上)

$('.button').click(function() {
    $('.knob').enable();
});

Edit: I ended up rewriting the source which binds/unbinds the mouse and touch events. 编辑:我最终重写了绑定/取消绑定鼠标和触摸事件的源。 The solution is not perfect so I leave the question open if someone perhaps have a better (cleaner) solution. 解决方案不是完美的,所以如果有人可能有更好(更干净)的解决方案,我将悬而未决。

html html

<input class="knobSlider" data-readOnly="true">
<button id="testBtn">clickHere</button>

script 脚本

in doc ready, 在准备好文档后,

$(".knobSlider").knob();
$("#testBtn").click(function(){
    $(".knobSlider").siblings("canvas").remove();
    if($(".knobSlider").attr("data-readOnly")=='true'){
        $(".knobSlider").unwrap().removeAttr("data-readOnly readonly").data("kontroled","").data("readonly",false).knob();
    }
    else{
        $(".knobSlider").unwrap().attr("data-readOnly",true).data("kontroled","").data("readonly",true).knob();
    }
});

For reference you can use my jsfiddle link > http://jsfiddle.net/EG4QM/ (check this in firefox, because of some external resource load problem in chrome) 作为参考,您可以使用我的jsfiddle链接> http://jsfiddle.net/EG4QM/ (在Firefox中进行检查,因为Chrome中存在一些外部资源加载问题)

If someone doesn't like how the accepted answer destroys and recreates the internal canvas element, then checkout my approach: 如果有人不喜欢被接受的答案如何破坏并重新创建内部canvas元素,那么请查看我的方法:

https://jsfiddle.net/604kj5g5/1/ https://jsfiddle.net/604kj5g5/1/

Essentially, check the draw() implementation (I also recommend listening on value changes in the draw method instead of the change and release , which work for and click and mousewheel events respectively, which imo is inconvenient). 本质上,检查一下draw()实现(我还建议您监听draw方法中的值更改,而不是监听changerelease ,后者分别适用于clickmousewheel事件,这对imo不方便。)

var $input = $("input");

var knobEnabled = true;
var knobPreviousValue = $input.val();

$input.knob({
  draw: function () { 
    if (knobPreviousValue === $input.val()) {
      return;
    }

    if (!knobEnabled) {
      $input.val(knobPreviousValue).trigger("change");
      return;
    }

    knobPreviousValue = $input.val();

    console.log($input.val()); 
  },
});

Try this to disable the control. 尝试执行此操作以禁用控件。 I'm still trying to find a way to enable it back 我仍在尝试找到一种方法来启用它

 $("#btnDisable").click(function(){
      $("#knob").off().prev().off();
    });

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

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