
[英]Knockout.js hasfocus causes value to be reset when losing focus before the throttle period expires
[英]Knockout JS: `hasFocus` always has Focus
我有一个单一的视图模型和两个模型。 对于每个模型,我试图将名称属性绑定到dom。 每个名称都有默认值,但我希望用户能够编辑这些值。 在Knockout文档之后,我使用了hasFocus方法。 我的问题是,点击后,我可以编辑,但当我点击时,焦点不会改变,我的阵列不会更新。 看起来我的模型editable
属性永远不会被设置为false
。 请参阅下面的相应HTML和JS。
那么我做错了什么? 这是我使用(无济于事)故障排除的清单......
editing
是可观察的。 edit
功能将editing
设置为true
。 editing
默认为false
<input>
具有value
绑定。 <i>
具有text
绑定。 editing
值 JSBin : http : //jsbin.com/fehoq/117/edit
function StudentModel(fullName) {
var _this = this;
this.fullName = ko.observable(fullName);
this.editing = ko.observable(false);
this.edit = function() {
_this.editing(true);
console.log(_this.editing());
};
...
}
<tbody>
<!-- ko foreach: students -->
<tr>
<td>
<input data-bind="value: fullName() + ' ' + ($index() + 1), visible: editing(), hasFocus: editing()"/>
<i data-bind="visible: !editing(), text: fullName() + ' ' + ($index() + 1), click: edit"> </i>
</td>
...
您可能希望将hasFocus
绑定到observable本身,以便可以将false值写回到它。 所以,你会想要:
hasFocus: editing
而不是hasFocus: editing()
在后者中,绑定仅接收值,并且无法返回到observable以写入它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.