简体   繁体   中英

How to get the value of a textbox onchange with knockout.js

I have the following code in knockout. The html is

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

The viewModel is

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());

How can I recover the value of searchLocation?

Messages in the console are

begin model

project5.js:15 model self.searchLocation

project5.js:6 focus self.searchLocation

you've defined searchLocation twice, once on self and again immediately after that. also, you should use the value binding instead of textInput

see this updated fiddle: http://jsfiddle.net/uete2Lsq/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM