Knockout.js 在Internet Explorer中按Enter键后如何更新observable

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

HTML代码


可观察

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