【发布时间】:2016-06-16 23:16:09
【问题描述】:
我有一个包含货币值的输入字段。当输入字段没有焦点时,我试图显示格式化的货币金额。输入字段类型设置为数字。虽然这在 Chrome、IE 和 Edge 上运行良好;在 Firefox 上,输入文本框在获取后立即失去焦点。这是简化的jsfiddle
https://jsfiddle.net/nkkfmLmc/2/
这与http://knockoutjs.com/documentation/hasfocus-binding.html 的示例 2 类似,但数字字段除外。如果我将输入类型从数字更改为文本(但也失去了所有优点),问题就解决了。 任何指针或解决方法将不胜感激。
HTML:
<p>
Name:
<b data-bind="visible: !editing(), text: '$'+amount(), click: edit"> </b>
<input type="number" min="0" max="100" step="1" data-bind="visible: editing, value: amount, hasFocus: editing" />
</p>
<p><em>Click the amount to edit it; click elsewhere to apply changes.</em></p>
JS:
function PersonViewModel(amount) {
// Data
this.amount = ko.observable(amount);
this.editing = ko.observable(false);
// Behaviors
this.edit = function() { this.editing(true) }
}
ko.applyBindings(new PersonViewModel(51.22));
【问题讨论】:
标签: javascript firefox knockout.js