【发布时间】:2015-02-13 20:51:30
【问题描述】:
我的 Jquery Slider 与 Knockout JS 结合使用时有一个小问题 我想通过可观察值绑定滑块最大选项。
这是我的滑块类:
<div>
<label for="Range1Slider">GRP:</label>
<input type="range" name="Range1Slider" id="Range1" data-track-theme="c" min="10" max="200" step="10" data-bind="value: value1, slider: value1" />
</div>
和我的 bindingHandler 用于获取和设置滑块值:
ko.bindingHandlers.slider = {
init: function (element, valueAccessor) {
// use setTimeout with 0 to run this after Knockout is done
setTimeout(function () {
// $(element) doesn't work as that has been removed from the DOM
var curSlider = $('#' + element.id);
// helper function that updates the slider and refreshes the thumb location
function setSliderValue(newValue) {
curSlider.val(newValue).slider('refresh');
}
// subscribe to the bound observable and update the slider when it changes
valueAccessor().subscribe(setSliderValue);
// set up the initial value, which of course is NOT stored in curSlider, but the original element :\
setSliderValue($(element).val());
// subscribe to the slider's change event and update the bound observable
curSlider.bind('change', function () {
valueAccessor()(curSlider.val());
});
}, 0);
},
update: function (element, valueAccessor) {
var newValue = ko.utils.unwrapObservable(valueAccessor());
if (isNaN(newValue)) newValue = 0;
var curSlider = $('#' + element.id);
// helper function that updates the slider and refreshes the thumb location
function setSliderValue(newValue) {
curSlider.val(newValue).slider('refresh');
}
}
};
【问题讨论】:
标签: javascript jquery jquery-mobile knockout.js