KnockoutJS:如何在用户复制“n"粘贴到文本字段后更新视图模型? [英] KnockoutJS: How to update view model after a user copy'n'paste into a text field?

查看:16
本文介绍了KnockoutJS:如何在用户复制“n"粘贴到文本字段后更新视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,该网站在用户在输入文本框中输入一些文本后自动将搜索结果填充到表格中(类似于谷歌即时搜索).

当用户通过添加

输入信息时,我设法让淘汰赛 js 更新视图模型

valueUpdate: 'afterkeydown'

到我的数据绑定属性中,但是,我还需要处理用户右键单击并将一些文本粘贴到文本框中的情况,所以我尝试了:

valueUpdate: ['afterkeydown','mouseup']

但这不起作用,当我尝试通过视图模型读取文本框的值时我一直在获取旧值,直到我退出输入文本框.>

有人知道我该如何解决这个问题吗?

奥斯卡

解决方案

您可以使用 valueUpdate:'input'.我已经测试它可以在 Opera、Firefox 和 Chrome 中工作.我在 Linux 机器上,所以我无法在 IE 中测试它.检查这个小提琴

更新:我现在已经在 IE8 中测试了它,但它不起作用.但使用以下似乎有效.

valueUpdate:['afterkeydown','propertychange','input']

感谢 Michael Best 对此的评论:) 我已经更新了小提琴

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:如何在用户复制“n"粘贴到文本字段后更新视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆