使用Knockout绑定处理IE 9和10的清除按钮

Ali*_*ned 10 internet-explorer input knockout.js

我有一个输入文本框绑定到淘汰赛js observable.

<input id="searchTextBox" class="searchTextBox" type="text" maxlength="25"
       title="Search" placeholder="Search"
       data-bind="value: GridVm.FilterText,
       valueUpdate: 'afterkeydown',
       disable: GridVm.Data().length == 0" />
Run Code Online (Sandbox Code Playgroud)

问题是当用户在IE中单击x时,FilterText observable不会更新.

我发现我可以移除x(参见链接问题中的屏幕截图),但这是最后的手段(我喜欢这个功能).这个论坛说,点击x时没有事件被触发.

是否有一个事件可以用来强制Knockout可观察的更新或在Knockout中执行此操作的好方法?

Haf*_*hor 11

如果你改变了

valueUpdate: 'afterkeydown'
Run Code Online (Sandbox Code Playgroud)

valueUpdate: 'input'
Run Code Online (Sandbox Code Playgroud)

它挂钩该事件以触发值更新.它总体上更好,因为它还处理基于剪贴板的操作和文本拖放操作.


Ali*_*ned 5

我用输入事件Knockout的事件绑定来计算它.这是我的JsFiddle使用下面的代码显示解决方案.

<input type="search" id="input1" data-bind="value: textForBox, valueUpdate: 'afterkeydown',
  event: { input: cleared }" />
var vm = {
    textForBox: ko.observable(),
    cleared: function (data, event) {
        if (event.currentTarget.value === '') {
           this.textForBox('');
        }
    }
};
ko.applyBindings(vm);
Run Code Online (Sandbox Code Playgroud)