我有三个滑块,我们称它们为一、二、三。滑块的值应该是one >= two >= three
。
我创建了一个小提琴,在创建滑块时设置了更改和滑动的回调:
$(document).ready(function() {
var oneValue = 5,
twoValue = 3,
threeValue =1;
var one = $('#one'),
two = $('#two'),
three = $('#three');
one.slider({
min: 0,
max: 10,
value: oneValue,
change: function(e, ui) {
oneValue = ui.value;
oneTest();
},
slide: function(e, ui) {
oneValue = ui.value;
oneTest();
}
});
two.slider({
min: 0,
max: 10,
value: twoValue,
change: function(e, ui) {
twoValue = ui.value;
twoTest();
},
slide: function(e, ui) {
twoValue = ui.value;
twoTest();
}
});
three.slider({
min: 0,
max: 10,
value: threeValue,
change: function(e, ui) {
threeValue = ui.value;
threeTest();
},
slide: function(e, ui) {
threeValue = ui.value;
threeTest();
}
});
function oneTest() {
console.log('oneTest');
if (oneValue < twoValue) {
two.slider('value', oneValue);
}
}
function twoTest() {
console.log('twoTest');
if (twoValue < threeValue) {
three.slider('value', twoValue);
}
if (twoValue > oneValue) {
one.slider('value', twoValue);
}
}
function threeTest() {
console.log('threeTest');
if (threeValue > twoValue) {
two.slider('value', threeValue);
}
}
});
小提琴在这里:http: //jsfiddle.net/RAGvr/3/。它像我期望的那样工作。
但是,我需要能够在创建滑块后附加事件处理程序。所以我创建了滑块,然后用 jQuery.on()监听change
和事件。slide
像这样:
$(document).ready(function() {
var oneValue = 5,
twoValue = 3,
threeValue =1;
var one = $('#one'),
two = $('#two'),
three = $('#three');
one.slider({
min: 0,
max: 10,
value: oneValue
});
two.slider({
min: 0,
max: 10,
value: twoValue
});
three.slider({
min: 0,
max: 10,
value: threeValue
});
one.on('change slide', function(e, ui) {
oneValue = ui.value;
oneTest();
});
two.on('change slide', function(e, ui) {
twoValue = ui.value;
twoTest();
});
three.on('change slide', function(e, ui) {
threeValue = ui.value;
threeTest();
});
function oneTest() {
console.log('oneTest');
if (oneValue < twoValue) {
two.slider('value', oneValue);
}
}
function twoTest() {
console.log('twoTest');
if (twoValue < threeValue) {
three.slider('value', twoValue);
}
if (twoValue > oneValue) {
one.slider('value', twoValue);
}
}
function threeTest() {
console.log('threeTest');
if (threeValue > twoValue) {
two.slider('value', threeValue);
}
}
});
Fiddle 在这里,http://jsfiddle.net/RAGvr/4/,但根本不像第一个那样工作。如果我移动滑块 3,则不会触发其他滑块的回调。我究竟做错了什么?