jQuery 文本框更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6139954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery textbox change event
提问by jcvandan
Does text input element not have a change event? When I attach a change event handler to a text input it is not being fired. Keyup is fired, but keyup is not sufficient for detecting a change as there are obviously other ways of entering text into an input.
文本输入元素没有更改事件吗?当我将更改事件处理程序附加到文本输入时,它不会被触发。Keyup 被触发,但 keyup 不足以检测更改,因为显然还有其他方法可以将文本输入到输入中。
采纳答案by jcvandan
There is no real solution to this - even in the links to other questions given above. In the end I have decided to use setTimeout
and call a method that checks every second! Not an ideal solution, but a solution that works and code I am calling is simple enough to not have an effect on performance by being called all the time.
对此没有真正的解决方案 - 即使在上面给出的其他问题的链接中也是如此。最后我决定使用setTimeout
和调用一个每秒检查一次的方法!不是一个理想的解决方案,但一个有效的解决方案和我调用的代码足够简单,不会因为一直被调用而对性能产生影响。
function InitPageControls() {
CheckIfChanged();
}
function CheckIfChanged() {
// do logic
setTimeout(function () {
CheckIfChanged();
}, 1000);
}
Hope this helps someone in the future as it seems there is no surefire way of acheiving this using event handlers...
希望这对将来的某人有所帮助,因为似乎没有使用事件处理程序实现这一目标的万无一失的方法......
回答by MikeM
The HTML4 spec for the <input>
elementspecifies the following script events are available:
<input>
元素的 HTML4 规范指定以下脚本事件可用:
onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup
onfocus, onblur, onselect, onchange, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup
here's an example that bind's to all these events and shows what's going on http://jsfiddle.net/pxfunc/zJ7Lf/
这是一个绑定到所有这些事件并显示发生了什么的示例http://jsfiddle.net/pxfunc/zJ7Lf/
I think you can filter out which events are truly relevent to your situation and detect what the text value was before and after the event to determine a change
我认为您可以过滤掉哪些事件与您的情况真正相关并检测事件前后的文本值以确定更改
回答by Pow-Ian
I have found that this works:
我发现这有效:
$(document).ready(function(){
$('textarea').bind('input propertychange', function() {
//do your update here
}
})
回答by ventaur
The change event only fires after the input loses focus (and was changed).
change 事件仅在输入失去焦点(并被更改)后触发。
回答by Zaheer Ahmed
回答by Yaseer Arafat
$('#input').on("input",function () {alert('changed');});
works for me.
为我工作。