KnockoutJS:如何在用户将'粘贴'到文本字段后更新视图模型? [英] KnockoutJS: How to update view model after a user copy'n'paste into a text field?
问题描述
我在一个网站上工作,该网站在用户在输入文本框中输入一些文本后(类似于google即时搜索),自动填充表格中的搜索结果。我设法让淘汰赛的js更新视图模型,因为用户通过添加
valueUpdate:'afterkeydown'
放入我的数据绑定属性,但是,我还需要处理用户右键单击并将一些文本粘贴到文本框的情况,所以我试过:
valueUpdate:['afterkeydown','mouseup']
但当我尝试通过视图模型读取文本框的值时,我无法获得旧值,直到我从输入文本框中跳出。
任何人都知道我该如何解决这个问题?
Oscar
您可以使用valueUpdate:'input'。我已经测试过它在Opera,Firefox和Chrome上工作。我在Linux上,所以我无法在IE中测试它。 检查这个小提琴
更新:我现在在IE8中测试它,它不起作用。但使用以下似乎工作。
valueUpdate:['afterkeydown','propertychange','input']
UPDATE okt 2014:由于kzh在下面的评论中提及,在Knockout的后续版本中.js添加了textInput绑定。这个绑定处理了这个场景,并且内置了浏览器怪癖,处理
http://knockoutjs.com/documentation/textinput- binding.html
I am working on a website that auto populates search result in a table after user entered some text in the input text box (similar to google instant search).
I managed to get knockout js to update view model as user enters information by adding
valueUpdate: 'afterkeydown'
into my data-bind attribute, however, I also need to handle the case where user right click and paste some text into the textbox so I tried:
valueUpdate: ['afterkeydown','mouseup']
but that didn't work and when I tried to read the value of the text box through the view model I kept getting the old value until I tab out of the input text box.
Anyone know how can I fix this?
Oscar
You can use valueUpdate:'input'. I have testet it to work in Opera, Firefox and Chrome. I'm on a Linux box, so I can't test it in IE. Check this fiddle
UPDATE: I have now testet it in IE8, and it doesn't work. But using the following seems to work.
valueUpdate:['afterkeydown','propertychange','input']
Thanks to Michael Best for his comment about this :) I have updated the fiddle
UPDATE okt 2014: As kzh mention in a comment below, in one of the later versions of Knockout.js the textInput binding was added. This binding handles this scenario and has build in browser quirks handling http://knockoutjs.com/documentation/textinput-binding.html
这篇关于KnockoutJS:如何在用户将'粘贴'到文本字段后更新视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!