Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我不能用knockout.js调用这个JavaScript函数_Javascript_Knockout.js - Fatal编程技术网

为什么我不能用knockout.js调用这个JavaScript函数

为什么我不能用knockout.js调用这个JavaScript函数,javascript,knockout.js,Javascript,Knockout.js,我试图通过单击按钮来调用此函数“callreach()”,但我无法这样做。有人能告诉我我做错了什么吗?带有数据绑定的第二个按钮工作正常 以下是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">&

我试图通过单击按钮来调用此函数“callreach()”,但我无法这样做。有人能告诉我我做错了什么吗?带有数据绑定的第二个按钮工作正常

以下是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>

谢谢

更新-1

这是修订版

直接调用ChangeCurrent()函数的ChangeCurrent按钮会更新DOM中的x1和x2值,但“再次调用”按钮不会更改它们。第二个问题是,滑块虽然有其属性数据绑定,但不会更新


有什么建议吗?

在“onclick”代码中没有使用knockout。因此,变量x1在alert正在运行的范围内不可用,但alert(“AAA”)至少应该可以工作?如果我注释掉最后一行,而不是取消注释这一行[if(FirstRun==true)…],那么一切都会停止工作;无法工作,因为x1未定义。在onDomready函数中声明的全局作用域中没有x1。电话也一样,那我该怎么办?将其封装在$(document).ready(function(){}中不起作用
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());