Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

javascript - 检测到对所有更改 <input type=“text”> (立即)使用JQuery(Detect all changes to a <input type=“text”> (immediately) using JQuery)

There are many ways the value of a <input type="text"> can change, including:(<input type="text">的值可以通过多种方式更改,包括:)

keypresses(按键) copy/paste(复制粘贴) modified with JavaScript(用JavaScript修改) auto-completed by browser or a toolbar(通过浏览器或工具栏自动完成) I want my JavaScript function to be called (with the current input value) any time it changes.(我希望我的JavaScript函数在每次更改时都被调用(使用当前输入值)。) And I want it to be called right away, not just when the input loses focus.(我希望立即调用它,而不仅仅是输入失去焦点时。) I'm looking for the cleanest and most robust way to do this across all browsers (using jQuery preferably).(我正在寻找最干净,最可靠的方法来在所有浏览器中执行此操作(最好使用jQuery)。) Example use case: On the Twitter Signup page, the username field's value gets shown in the url " http://twitter/ username " below it.(用例示例:在Twitter Signup页面上, 用户名字段的值显示在其下方的URL“ http:// twitter / username ”中。)   ask by Dustin Boswell translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This jQuery code catches immediate changes to any element, and should work across all browsers:(此jQuery代码可捕获对任何元素的立即更改,并且应可在所有浏览器上使用:)

$('.myElements').each(function() { var elem = $(this); // Save current value of element elem.data('oldVal', elem.val()); // Look for changes in the value elem.bind("propertychange change click keyup input paste", function(event){ // If value has changed... if (elem.data('oldVal') != elem.val()) { // Updated stored value elem.data('oldVal', elem.val()); // Do action .... } }); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...