Knockout js jquery range slider && 2 inputs

本小妞迷上赌 提交于 2019-12-12 16:06:10

问题


I need a some help. I have a code http://jsfiddle.net/ZNvWR/19/. I'm newbie in knockout, and I can't find any solution.

So, how to rewrite this code for getting working inputs (change values in inputs changes slider values)?

<div data-bind="jqSlider: percent, jqOptions: { min: 0, max: 100, range:true }"></div>
<hr/>
Percent: <input data-bind="value: percent()[0]" />
Percent: <input data-bind="value: percent()[1]" />

ko.bindingHandlers.jqSlider = {
  init: function(element, valueAccessor, allBindingsAccessor) {
    //initialize the control
    var options = allBindingsAccessor().jqOptions || {};
    $(element).slider(options);

    //handle the value changing in the UI
    ko.utils.registerEventHandler(element, "slide", function() {
        //would need to do some more work here, if you want to bind against non-observables
        var observable = valueAccessor();
        observable($(element).slider("values"));
    });

    //handle disposal (if KO removes by the template binding)
    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
        $(element).slider("destroy");
    });
  },
  //handle the model value changing
  update: function(element, valueAccessor) {
    var value = ko.utils.unwrapObservable(valueAccessor());
    $(element).slider("values", value);   
  }
};

var viewModel = {
    percent: ko.observableArray([10,50])
};
ko.applyBindings(viewModel)

回答1:


I just helped antoher SO user with a slider, it can be altered like this to do what you want

http://jsfiddle.net/N9uwx/3/

<input data-bind="value: min" /><input data-bind="value: max" /><div data-bind="slider: { min: min, max: max }, sliderOptions: {min: 0, max: 100, step: 1}"></div>


来源:https://stackoverflow.com/questions/13682691/knockout-js-jquery-range-slider-2-inputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!