首頁 >web前端 >js教程 >jquery行動端鍵盤keyup失效的解決辦法分享

jquery行動端鍵盤keyup失效的解決辦法分享

黄舟
黄舟原創
2017-06-27 14:23:192776瀏覽

最近做的觸控版的專案中遇到監聽input的值,使用keyup,手機上的鍵盤按下其它鍵沒問題,但是刪除鍵卻監聽不到,在網路上找到下面的解決方法。

(原文出自:http://blog.csdn.net/kongjiea/article/details/40186121)

#搜尋框依據用戶輸入的值即時檢索,一開始自然而然想到keyup,在拼音狀態時,啥問題也沒有,

問題1:切換到中文輸入法,問題出來了,keyup事件不靈便了,後來在網路上搜了下,找到了思路,

問題2:微信公眾平台開發時,客戶提需求“輸入框中輸入內容時,輸入框後邊顯示清除按鈕,清除輸入框中的內容”,使用“keyup”事件時在中文輸入法下部分按鍵keyup事件無效,


#方法一:主要是給搜尋框註冊focus事件,隔個時間去檢索下,貼出程式碼

<script language="javascript" type="text/javascript" src="jquery.js"></script>  
    <script>  
  
    $(function () {  
        $(&#39;#wd&#39;).bind(&#39;focus&#39;,filter_time);  
    })  
  
    var str = &#39;&#39;;  
    var now = &#39;&#39;  
    filter_time = function(){  
        var time = setInterval(filter_staff_from_exist, 100);  
        $(this).bind(&#39;blur&#39;,function(){  
            clearInterval(time);  
        });  
    };  
  
    filter_staff_from_exist = function(){  
        now = $.trim($(&#39;#wd&#39;).val());  
        if (now != &#39;&#39; && now != str) {  
            console.log(now);  
        }  
        str = now;  
    }  
    </script>

方法二:用input 和 propertychange事件可以解決, 

本人測試只能用dom2的綁定方法使用 如document.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 = &#39;input&#39;;  
            if (navigator.userAgent.indexOf("MSIE") != -1){  
                bind_name = &#39;propertychange&#39;;  
            }  
            $(&#39;#inputorp_i&#39;).bind(bind_name, function(){  
                $(&#39;#inputorp_s&#39;).text($(this).val());  
            })  
        </script>  
    </p>  
</body>  
</html>

可是也有人說用jq方式綁定即可如:

$(&#39;#input&#39;).bind(&#39;input propertychange&#39;, function() {  
                alert("....")  
            });

或原生:

<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>

最後要注意的是:oninput 和onpropertychange 這兩個事件在IE9 中都有個小BUG,那就是透過右鍵選單選單中的剪切和刪除指令刪除內容的時候不會觸發,而IE 其他版本都是正常的,目前還沒有很好的解決方案。不過 oninput & onpropertychange 仍然是監聽輸入框值變化的最佳方案..

#

以上是jquery行動端鍵盤keyup失效的解決辦法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn