2013-05-07 71 views
9

我有一個輸入文本框綁定到knockout js觀察。處理IE 9&10的清除按鈕與敲除綁定

<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" /> 

的問題是,當用戶點擊在x在IE中可觀察到的FilterText不更新。

我發現我可以remove the x(見鏈接的問題截屏),但是這是一個不得已的(我喜歡的功能)。 This forum says there is no event fired when the x is clicked

是否有一個事件可以用來強制Knockout可觀察更新或在Knockout中執行此操作的好方法?

+0

嘗試點擊綁定 – CodeThug 2013-05-07 20:13:06

回答

11

如果你只需要改變

valueUpdate: 'afterkeydown' 

valueUpdate: 'input' 

它掛鉤該事件觸發值更新。它總體上更好,因爲它還處理基於剪貼板的操作和文本拖放操作。

+0

好的發現!這也適用於我。我正在改變你的答案並更新我的小提琴。 – Aligned 2013-06-26 19:07:18

+0

是完美的解決方案。 – 2017-12-12 13:07:37

+0

我發現將data-bind從value更改爲textInput可以很好地解決問題。爲了阻止它在每個關鍵字之後更新,我將其限制爲可觀察的速率限制:http://knockoutjs.com/documentation/rateLimit-observable.html以獲取詳細信息。 – 2017-12-12 14:37:53

4

我想出了使用input eventKnockout's event binding。下面是代碼my JsFiddle showing the solution

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