我试图通过点击按钮调用此函数“CallAgain()”,但我无法这样做。有人可以告诉我我做错了什么吗?带数据绑定的第二个按钮工作正常。
这是HTML:
<input id="slid" data-bind="attr: {min: 1, max: MaxPage}, value: CurrPage" type="range" data-highlight="true">
<hr/>
X1=<span data-bind="text: CurrPage"></span></br>
X2=<span data-bind="text: MaxPage"></span>
<hr>
<button onclick="alert(x1);">Call Again</button>
<button data-bind="click: changeCurrent">Change current</button>
这是JS代码:
var x1 = 8,
x2 = 10;
function CallAgain() {
alert("AAA");
var vm = new AppViewModel();
//if ( FirstRun == true ) ko.applyBindings(vm);
x1++;
x2++;
vm.CurrPage(x1);
vm.MaxPage(x2);
vm.changeCurrent();
FirstRun = false;
}
function ViewModel () {
this.CurrPage = ko.observable(x1);
this.MaxPage = ko.observable(x2);
this.CurrPage.subscribe(function (value) {
x1 = value;
});
this.changeCurrent = function () {
x1++;
x2++;
this.CurrPage(x1);
this.MaxPage(x2);
alert( this.CurrPage() );
$('#slid').slider("refresh");
}
};
ko.applyBindings(new ViewModel());
Here is the jsfiddle link for the code
由于
更新-1
这是修订后的jsfiddle
ChangeCurrent按钮,直接调用changeCurrent()函数,更新x1&amp; DOM中的x2值但“再次呼叫”按钮不会更改它们。第二个问题是滑块不会更新,尽管它的数据绑定属性。
请问任何建议?