最近做的触屏版的项目中遇到监听input的值,使用keyup,手机上的键盘按下其它键没问题,但是删除键却监听不到,在网上找到下面的解决方法。
搜索框依据用户输入的值实时检索,一开始自然而然想到keyup,在拼音状态时,啥问题也没有,
问题1:切换到中文输入法,问题出来了,keyup事件不灵便了,后来在网上搜了下,找到了思路,
问题2:微信公众平台开发时,客户提需求“输入框中输入内容时,输入框后边显示清除按钮,清除输入框中的内容”,使用“keyup”事件时在中文输入法下部分按键keyup事件无效,
方法一:主要是给搜索框注册focus事件,隔个时间去检索下,贴出代码
$(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;
}
方法二:用 input 和 propertychange事件可以解决
//先判断浏览器是不是万恶的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());
})
方法二并不是适用比较老的浏览器