绑定前设置滑块值

时间:2016-10-19 08:26:31

标签: jquery jquery-mobile knockout.js

我试图将滑块绑定到某个值,就像在此fiddle中一样。但相反,我得到了这个:fiddle。 我的js-part看起来像这样:

ko.bindingHandlers.slider = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
             var value = valueAccessor();
       this.slider = $(element).attr("value", value());
    }
 };

 var testResult =  function (score){
                var self = this;
        self.score = ko.observable(score);
        self.id = ko.observable("slider-new");
        self.min = ko.observable(10);
        self.max = ko.observable(100); 
    };
    var result = new testResult(50);
   ko.applyBindings(result); 

1 个答案:

答案 0 :(得分:0)

这足以强制更新:

ko.bindingHandlers.slider = {
  init: function(element) {
    $(element).trigger("change");
  },
};