JQuery UI滑块在0时不工作?

问题描述:

我想在我的流星应用程序中创建一个带有JQuery UI的滑块,可能的值在0到1之间,步长为0.001,我不希望滑块具有默认值,也不显示滑块的句柄,直到用户首先决定一个值。JQuery UI滑块在0时不工作?

Template.game_task.onCreated(()=>{ 
    if (!Session.get("sliderValue")){ 
     Session.setPersistent('sliderValue', -1) 
    } 

}); 

Template.game_task.onRendered(()=> { 

    /////// the slider stuff ////// 
    this.$("#slider").slider({ 
     min: -0.0000, 
     max: 1, 
     step: 0.01, 
     slide: function(event, ui) { 
      $('#slider > .ui-slider-handle').show(); 
      Session.setPersistent('sliderValue',ui.value) 
     }, 
     change: function(event, ui) { 
      //save value to collection 
     } 
    }); 

    if (Session.get("sliderValue") === -1){ 
     //if no value is set, then hide the handle 
     $('#slider > .ui-slider-handle').hide(); 
    } 

}); 

Template.game_task.helpers({ 
    getSliderValue(){ 
     const sliderValue = Session.get('sliderValue'); 
     if (sliderValue){ 
      console.log('slider value to be returned',sliderValue) 
      return sliderValue; 
     } 
    }, 

}); 

,这是HTML:

<div class='row'> 
    <div id="slider" class="custom-handle"></div> 
    <span id="min">0</span> 
    <span class="quarter">0.25</span> 
    <span class="quarter">0.5</span> 
    <span class="quarter">0.75</span> 
    <span id="max">1</span> 
    <p class="contributionText">Your guess: {{getSliderValue}}</p> 
</div> 

然而,滑块在下限给人一种怪异的行为(当我拉滑块向0)。当我进入0时,我得到小的值(即0.01,0.016),然后我得到的值为空(根据我的getSliderValue()函数{{getSliderValue}})。

enter image description here

所以我从来没有能挑到0 ..这不会在高端发生但正如我去1

enter image description here

另外,如果我改变最小值为0以外的东西,我得到正确的行为在下限(但在我的情况下,它必须是0)。

你的问题是这样的代码:

if (sliderValue){ 
    ... 
} 

由于你的价值0这种状态就不会true。你应该把它改成这样:

if (sliderValue != null){ 
    ... 
} 
+0

改进:'如果(sliderValue == NULL && typeof运算sliderValue == '未定义'!)' – Jankapunkt

+0

@Jankapunkt这正是我的代码意味着:)如果你看在这里你会看到'!= null',这就等于你的代码。 – Styx

+0

Sry因为混淆,我忘了,对于负面比较,你不需要严格的类型检查。 sliderValue!= null在这种情况下是足够的。 – Jankapunkt