JavaScript and jQuery: Is there any PROPER onchange event?

大兔子大兔子 提交于 2019-12-25 14:58:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!