Jquery ui slider:基于复选框设置步长
Jquery ui slider: Set step based on checkbox
我正在尝试设置我的jquery ui滑块,以便如果我的复选框未被选中,它将具有0.1的步长值,或者如果复选框被选中,则步长值为1。我对jquery的经验非常有限,我还在学习,所以任何帮助都会很感激。以下是我到目前为止所做的尝试。JSFIDDLE
JS
//Setup for jquery ui slider
harmonicSlider = $("#harmonicSlider").slider({
value: 1,
min: 1,
max: 5,
step: 1,
//Updates the slider textbox with the current value of the slider.
slide: function(event, ui){
var harmonicSnap = $("harmonicSnap");
setSliderBoxValue(ui.value);
if(harmonicSnap.checked){
$(this).slider("option", "step", 1);
}else{
$(this).slider("option", "step", 0.1);
}
var stepping = $(this).slider('option', 'step');
console.log("Step: " + stepping);
},
//Gets the slider value after a change to the slider is made.
change: function (event, ui){
getSliderValue(ui.value);
}
});
//Grabs the slider value and returns it.
function getSliderValue(slideVal){
console.log("Slider Value: " + slideVal);
}
//Sets the sliderValueBox to the current value of the slider.
function setSliderBoxValue(currentValue){
$("#sliderValueBox").val(currentValue);
}
//Sets the step of the slider depending on if the harmonics checkbox is checked or not.
function setSliderIncrement(){
var harmonicSnap = document.getElementById("harmonicSnap");
//var slider = $("harmonicsSlider").slider();
if(harmonicSnap.checked){
$("harmonicsSlider").slider("option", "step", 1);
console.log("Step: " + $("harmonicsSlider").slider("option", "step").val());
}else{
$("harmonicsSlider").slider("option", "step", 0.1);
//step = $("harmonicsSlider").slider("option", "step");
console.log("Step: " + $("harmonicsSlider").slider("option", "step").val());
}
}
//Loads the harmonic slider selector and slider labels
function loadHarmonicSlider(){
var sliderValTextBox = $("sliderValueBox");
harmonicSlider.each(function() {
//Add labels to slider specified by the min, max, and step values
var options = $(this).data().uiSlider.options;
var values = options.max - options.min;
//Spacing for value labels
for(var i = 0; i <= values; i++){
var element = $("<label>" + (i + 1) + "</label>").css("left", (i / values * 100) + "%");
$("#harmonicSlider").append(element);
}
});
}
HTML <form>
<div id="harmonicSliderLabel">n:</div>
<div id="harmonicSlider"></div>
<div id="sliderInputs">
<input type="text" id="sliderValueBox" value="1">
<input type="checkbox" id="harmonicSnap" value="Snap to harmonics" onchange="setSliderIncrement();" checked>Snap to harmonics
</div>
</form>
您的snap选择器是错误的。你漏掉了# char.
你也应该使用harmonicSnap.is(":checked")来检查它是否被选中。
jsfiddle
var harmonicSnap = $("#snap");
setSliderBoxValue(ui.value);
if (harmonicSnap.is(":checked")) {
$(this).slider("option", "step", 1);
} else {
$(this).slider("option", "step", 0.1);
}
代码中到处都缺少id
var harmonicSnap = $("harmonicSnap");
$("harmonicsSlider").slider("option", "step", 1);
$("harmonicsSlider").slider("option", "step", 0.1);
都需要加#
这是我的小提琴:http://jsfiddle.net/h6fgb12v/1/
我删除了一些损坏的控制台日志,通过jquery而不是内联绑定复选框更改,并从幻灯片内部删除了步骤更改(因为您将其绑定在复选框更改上)。缺少id仍然会导致一些错误,但这应该会让你找到正确的方向
HTML
<div id="slider"></div>
<div id="sliderInputs">
<input type="text" id="sliderValueBox" value="1">
<input type="checkbox" id="snap" checked>Snap to whole numbers
</div>
JS
<script>
$(function() {
var harmonicSlider = $("#slider").slider({
value: 1,
min: 1,
max: 5,
step: 1,
slide: function (event, ui) {
setSliderBoxValue(ui.value);
},
change: function (event, ui) {
getSliderValue(ui.value);
}
});
function getSliderValue(slideVal){
console.log("Slider Value: " + slideVal);
}
function setSliderBoxValue(currentValue){
$("#sliderValueBox").val(currentValue);
}
$('#snap').change(function() {
var step = $(this).is(":checked")?1:0.1;
$("#slider").slider({step: step});
});
$('#snap').change();
});
我很抱歉。但是我错更新了fiddle
相关文章:
- JQuery Datatables将复选框设置为选中
- 我无法将 MVC 复选框设置为在 jquery 中选中
- 在 Zkoss 中选中的复选框设置为禁用(真),但我希望复选框和勾选的可见性保持不变
- jQuery:根据另一个复选框设置选择框的值
- 在放入表单数据之前,将HTML复选框设置为字符串
- 为复选框设置 Cookie
- 如何为单选按钮和复选框设置禁用/只读功能
- 将“已创建”复选框设置为选中不起作用
- 如何为动态添加的复选框设置动态启用/禁用
- 将复选框设置为使用jQuery选中
- 将第一个复选框设置为选中,其他复选框从客户端的复选框列表中禁用
- 为不确定复选框设置自定义初始值只有在单击后才能工作
- 使用angularjs将复选框设置为选中时导致的错误
- Jquery验证多个复选框设置错误位置
- 如何在单击“取消”时将复选框设置为未选中?
- 当php选中复选框设置时,调用JS函数
- 将前2行输入字段的复选框设置为选中
- 选中复选框设置属性,否则删除属性
- Jquery ui slider:基于复选框设置步长
- AngularJS-当Value为true时,将复选框设置为选中