HTML文本输入onchange事件 [英] html text input onchange event
问题描述
是否有一种方法可以实现文本更改事件以检测html输入文本字段上的文本更改?可以使用按键事件(按下按键等)来模拟此操作,但它确实没有性能和困难,有没有更好的方法?
is there a way to implement an text change event to detect text change on a html input text field?
It's possible to simulate this using key events(key pressed etc), however it's really not performant and difficult, is there a better way?
推荐答案
当我做这样的事情时,我使用onKeyUp事件。
When I'm doing something like this I use the onKeyUp event.
<script type="text/javascript">
function bar() {
//do stuff
}
<input type="text" name="foo" onKeyUp="return bar()" />
但是如果您不想使用html事件,您可以尝试使用jQuerys .change( )方法
but if you don't want to use a html event you could try to use jQuerys .change() method
$('.target').change(function() {
//do stuff
});
在这个例子中,输入必须有一个类target
in this example the input would have to have a class "target"
如果您打算让多个文本框在您的文本被更改并且您需要他们的数据时做同样的事情,那么您可以这样做:
if you're going to have multiple text boxes that you want to have do the same thing when their text is changed and you need their data then you could do this:
$('.target').change(function(event) {
//do stuff with the "event" object as the object that called the method
)};
这种方式可以使用相同的代码,为多个文本框使用相同的类而不必重写任何代码。
that way you can use the same code, for multiple text boxes using the same class without having to rewrite any code.
这篇关于HTML文本输入onchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!