如何使用knockout.js获取文本框onchange的值

时间:2015-10-09 17:57:06

标签: javascript events input knockout.js

我在淘汰赛中有以下代码。 html是

<p id="inputField">
    What do you want to search?: 
    <input data-bind="
        textInput: searchLocation, 
        event: {focus:  focusHandler}"/>
</p>

viewModel是

function  NeighborhoodModel() {
    var self = this;

    self.focusHandler = function(){
        console.log("focus self.searchLocation "+searchLocation());
    };

    self.searchLocation= ko.observable("");
    searchLocation = ko.observable("");
    self.locations = ko.observableArray([]);
    self.nameLocation = ko.observable(" ");
    console.log(" begin model");
    console.log("model self.searchLocation "+searchLocation());

};

ko.applyBindings(new NeighborhoodModel());

如何恢复searchLocation的值?

控制台中的消息是

  

开始模特

     

project5.js:15 model self.searchLocation

     

project5.js:6焦点自我.searchLocation

1 个答案:

答案 0 :(得分:1)

您已经定义了两次searchLocation,一次是自我,之后是另一次。另外,您应该使用value绑定而不是textInput

看到这个更新的小提琴:http://jsfiddle.net/uete2Lsq/

<p id="inputField">
    What do you want to search?: 
    <input data-bind="
        value: searchLocation, 
        event: {focus:  focusHandler, blur: focusHandler}"/>
</p>