2015-10-09 57 views
0

我在淘汰賽中獲得了以下代碼。在HTML是如何通過knockout.js獲取文本框onchange的值

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

視圖模型是

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模型self.searchLocation

project5.js:6的焦點self.searchLocation

回答

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>