In a recent touch screen version project, I encountered the problem of monitoring the input value. Using keyup, it was no problem to press the other keys on the keyboard on the mobile phone, but the delete key could not be monitored. Find the solution below online.
(Original text from: http://blog.csdn.net/kongjiea/article/details/40186121)
The search box is based on the user The input value is retrieved in real time. At first, I naturally think of keyup. In Pinyin state , there is no problem.
Question 1: Switch to Chinese input Method, the problem came out, the keyup event was not working properly. Later, I searched on the Internet and found the idea,
Question 2: During the development of the WeChat public platform, a customer asked for "When entering content in the input box, a clear button will be displayed behind the input box to clear the content in the input box." When using the "keyup" event, some keyup events will occur under the Chinese input method. Invalid,
Method 1: Mainly register the focus event for the search box and retrieve it at intervals , post the code
<script language="javascript" type="text/javascript" src="jquery.js"></script> <script> $(function () { $('#wd').bind('focus',filter_time); }) var str = ''; var now = '' filter_time = function(){ var time = setInterval(filter_staff_from_exist, 100); $(this).bind('blur',function(){ clearInterval(time); }); }; filter_staff_from_exist = function(){ now = $.trim($('#wd').val()); if (now != '' && now != str) { console.log(now); } str = now; } </script>
Method 2: Use input and propertychange events to solve the problem,
I can only use the dom2 binding method for testing Such asdocument.getElementById('box').addEventListener('input',function(){...dosomething...},false);
html> <head> <script type="text/javascript" src="http://www.zlovezl.cn/static/js/jquery-1.4.2.min.js"></script> </head> <body> <p> 使用oninput以及onpropertychange事件检测文本框内容: </p> <p> <input type="text" name="inputorp_i" id="inputorp_i" autocomplete="off"/> <span id="inputorp_s"></span> <script type="text/javascript"> //先判断浏览器是不是万恶的IE,没办法,写的东西也有IE使用者 var bind_name = 'input'; if (navigator.userAgent.indexOf("MSIE") != -1){ bind_name = 'propertychange'; } $('#inputorp_i').bind(bind_name, function(){ $('#inputorp_s').text($(this).val()); }) </script> </p> </body> </html>
But some people say to use jq method Binding can be as follows:
$('#input').bind('input propertychange', function() { alert("....") });
or native:
<script type="text/javascript"> // Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9 function OnInput (event) { alert ("The new content: " + event.target.value); } // Internet Explorer function OnPropChanged (event) { if (event.propertyName.toLowerCase () == "value") { alert ("The new content: " + event.srcElement.value); } } </script> <body> <input type="text" oninput="OnInput (event)" onpropertychange="OnPropChanged (event)" value="Text field" /> </body>
Finally, please note The is :oninput Both events, onpropertychange and onpropertychange, have a small bug in IE9, that is, they will not be triggered when deleting content through the cut and delete commands in the right-click menu. However, other versions of IE are normal, and it is not very good at the moment. s solution. However, oninput & onpropertychange is still the best way to monitor input box value changes..
The above is the detailed content of Sharing the solution to the failure of jquery mobile keyboard keyup. For more information, please follow other related articles on the PHP Chinese website!