gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:41:21 25 4
gpt4 key购买 nike

我在 knockout 中有以下代码。 html 是

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

View 模型是

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 的值?

控制台中的消息是

begin model

project5.js:15 model self.searchLocation

project5.js:6 focus self.searchLocation

最佳答案

您已经定义了 searchLocation 两次,一次是在 self 上定义的,另一次是在之后立即定义的。另外,您应该使用 value 绑定(bind)而不是 textInput

查看此更新的 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>

关于javascript - 如何使用 knockout.js 获取文本框 onchange 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com