如何同步滑块?价值观?

发布于 2024-12-06 20:43:21 字数 1297 浏览 0 评论 0原文

我使用 jquery mobile sliders:

  <div data-role="fieldcontain">
    <input type="range" name="slider1" id="slider1" value="0" min="0" max="255"  />
  </div><br><br>
  <div data-role="fieldcontain">
    <select name="switcher1" id="switcher1" data-role="slider">
      <option value="0">Off</option>
      <option value="255">On</option>
    </select> 
  </div>      

它应该按以下方式工作:

  1. 如果触摸 switcher1,则 slider1 值应设置为 0 或 255;
  2. 如果 slider1 值发生更改,则 (a) 如果它 = 0,则 switcher1 应设置为 0,(b) 否则 switcher1 值应设置为 255。

我尝试使用 change 事件(同样用于 switcher1 change()):

var slider1_val = "0";
$('#slider1').change(function () {
    sVal = $(this).val();
    if (slider1_val !== sVal) {
        $("#slider1").val(sVal).slider("refresh");
        if (sVal == 0) {
            $("#switcher1").val(0).slider("refresh");
        } else {
            $("#switcher1").val(255).slider("refresh");
        }
        slider1_val = sVal;
    }
});

但看起来每次调用 refresh 都会调用 change事件,所以我变得无限 环形。

I use jquery mobile sliders:

  <div data-role="fieldcontain">
    <input type="range" name="slider1" id="slider1" value="0" min="0" max="255"  />
  </div><br><br>
  <div data-role="fieldcontain">
    <select name="switcher1" id="switcher1" data-role="slider">
      <option value="0">Off</option>
      <option value="255">On</option>
    </select> 
  </div>      

It should work in the following way:

  1. if switcher1 is touched, then slider1 value should be set either to 0 or to 255;
  2. if slider1 value is changed, then (a) if it is = 0, then switcher1 should be set to 0, (b) else switcher1 value should be set to 255.

I've tried to do so with change event (the same is used for switcher1 change()):

var slider1_val = "0";
$('#slider1').change(function () {
    sVal = $(this).val();
    if (slider1_val !== sVal) {
        $("#slider1").val(sVal).slider("refresh");
        if (sVal == 0) {
            $("#switcher1").val(0).slider("refresh");
        } else {
            $("#switcher1").val(255).slider("refresh");
        }
        slider1_val = sVal;
    }
});

But looks like each call of refresh calls change event, so I am getting infinite loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-12-13 20:43:22

它应该按以下方式工作:

  1. 如果 switcher1 被触摸,则 slider1 值应设置为 0 或 255;
  2. 如果 slider1 值发生更改,则 (a) 如果它 = 0,则 switcher1 应设置为 0,(b) 否则 switcher1 值应设置为 255。

我尝试使用更改事件来执行此操作(同样用于 switcher1 change()):

事实上,您有两个非常不同的标准来更改每个控件,这应该提示您更改事件处理程序也应该不同。使用相同的处理程序会导致您遇到的无限循环。下面的代码考虑了您提供的严格更改标准。请注意,slider1 更改处理程序在需要更改时更改 switcher1(根据您的条件),而不是每次调用它时。另请注意,在 slider1 更改处理程序中,调用刷新之前设置 switcher1_val,以便在 .slider('refresh') < em>确实调用更改处理程序,更改处理程序不会执行任何操作,因为 switcher1_val 已经更新。

var linkSliders = function(sliderId, switcherId){
    var slider = $('#'+sliderId),
        switcher = $('#'+switcherId);
    var min = Math.min(switcher[0].options[0].value, switcher[0].options[1].value),
        max = Math.max(switcher[0].options[0].value, switcher[0].options[1].value);
    var sliderVal = switcherVal = min;

    // set the slider min/max to be the same as the switcher's, just in case they're different
    slider.attr('max', max).attr('min', min).slider('refresh');  
    slider.change(function(){
        var sVal = $(this).val();
        if(sliderVal != sVal){
            if( sVal == min && switcherVal!=min){
                switcherVal=min;
                switcher.val(min).slider('refresh');
            }else if(sVal>min && switcherVal!=max){
                switcherVal=max;
                switcher.val(max).slider('refresh');
            }
            sliderVal = sVal;
        }
    });  

    switcher.change(function(){
        var sVal = $(this).val();
        if(switcherVal != sVal){
            slider.val(sVal).slider('refresh');
            switcherVal = sVal;
        }
    });
};

linkSliders('slider1','switcher1');

查看实例。

希望这会有所帮助。


更新:根据要求,该示例已被修改以使其更加通用。

It should work in the following way:

  1. if switcher1 is touched, then slider1 value should be set either to 0 or to 255;
  2. if slider1 value is changed, then (a) if it is = 0, then switcher1 should be set to 0, (b) else switcher1 value should be set to 255.

I've tried to do so with change event (the same is used for switcher1 change()):

The fact that you have two very different criteria for changing each control should tip you off that the change event handlers should be different as well. Using the same handlers leads to the infinite loop you are experiencing. The code below accounts for the strict change criteria you've provided. Note that the slider1 change handler changes switcher1 only if it needs to be changed (based on your criteria), not every time it is called. Also, note that in the slider1 change handler, switcher1_val is set before calling refresh, so that in case .slider('refresh') does call the change handler, the change handler will not do anything, because switcher1_val is already updated.

var linkSliders = function(sliderId, switcherId){
    var slider = $('#'+sliderId),
        switcher = $('#'+switcherId);
    var min = Math.min(switcher[0].options[0].value, switcher[0].options[1].value),
        max = Math.max(switcher[0].options[0].value, switcher[0].options[1].value);
    var sliderVal = switcherVal = min;

    // set the slider min/max to be the same as the switcher's, just in case they're different
    slider.attr('max', max).attr('min', min).slider('refresh');  
    slider.change(function(){
        var sVal = $(this).val();
        if(sliderVal != sVal){
            if( sVal == min && switcherVal!=min){
                switcherVal=min;
                switcher.val(min).slider('refresh');
            }else if(sVal>min && switcherVal!=max){
                switcherVal=max;
                switcher.val(max).slider('refresh');
            }
            sliderVal = sVal;
        }
    });  

    switcher.change(function(){
        var sVal = $(this).val();
        if(switcherVal != sVal){
            slider.val(sVal).slider('refresh');
            switcherVal = sVal;
        }
    });
};

linkSliders('slider1','switcher1');

See the live example.

Hope this helps.


Update: As requested, the example has been modified to make it more general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文