Knockout.js 在Internet Explorer中按Enter键后如何更新observable
HTML代码Knockout.js 在Internet Explorer中按Enter键后如何更新observable,knockout.js,data-binding,typescript,Knockout.js,Data Binding,Typescript,HTML代码 可观察 nextName:KnockoutComputed; currentName:KnockoutObservable; 构造函数() { this.nextName=ko.pureComputed( { 读取:()=>this.currentName(), 写入:(值:字符串)=> { 此.currentName(值); 如果((值)和&value.trim().length>0) 这是runSearch(); } },这个); } 用户按enter键后,搜索将在Chr
可观察
nextName:KnockoutComputed;
currentName:KnockoutObservable;
构造函数()
{
this.nextName=ko.pureComputed(
{
读取:()=>this.currentName(),
写入:(值:字符串)=>
{
此.currentName(值);
如果((值)和&value.trim().length>0)
这是runSearch();
}
},这个);
}
用户按enter键后,搜索将在Chrome中正常执行,但不会在Internet Explorer中执行。我尝试将绑定与
valueUpdate:'afterkeydown'
一起使用,但这将在每个字符后执行搜索-这不是我试图实现的。您可以使用textInput
绑定,并为enterkey
创建一个自定义敲除绑定,以执行所需操作:
textInput
在viewmodel
属性和元素的值。它为自动完成
、拖放
和剪贴板
事件提供实例更新。然后,您可以使用enterkey
binding在用户每次按下enter键时调用您的函数,这在IE中也应该起作用
<input type="text" data-bind="textInput: nextName,enterkey: function() {runSearch()}">
工作示例:我使用了这个polyfill-根本问题是IE在按下回车
键时不会触发更改
事件
var isIE = !!document.documentMode;
if (isIE) {
$("body").on("keydown", "input[type='text']", function (e) {
// Ensure 'value' binding is fired on enter in IE
if ((e.keyCode || e.which) === 13) {
$(this).blur();
}
});
}
var isIE = !!document.documentMode;
if (isIE) {
$("body").on("keydown", "input[type='text']", function (e) {
// Ensure 'value' binding is fired on enter in IE
if ((e.keyCode || e.which) === 13) {
$(this).blur();
}
});
}