问题
As we all know 'onchange' event will fire ONLY when the input will loose its focus. But the web apps often requires to do something immediately if the input changes its value and without difference how those changes happens: by users keyboard, mouse or other script... Of course I can declare a function which will fire 100 times a second and check the value of needed elements and do something if value changed. But it looks weird: first it will take more power from users browser, second 1/100 of a second can be insufficient in some cases and so on... It is not beautifully at last!
Do you have any better ideas?
回答1:
You can listen for multiple event's to be sure
$('input').on("keyup input paste propertychange", function(event){
// do stuff
});
回答2:
Use keyup
- it will fire straight away rather than when the input has lost focus:
$("#input").keyup(function() {
});
回答3:
On modern browsers, you should use oninput event:
$("input").on('input',function() {
//code here
});
来源:https://stackoverflow.com/questions/16712166/javascript-and-jquery-is-there-any-proper-onchange-event