Jquery preventDefault 不适用于 android 4.4 默认浏览器 [英] Jquery preventDefault not working on android 4.4 default browser
问题描述
我正在使用一个基于 angular 和 jquery 的网站.我有一个用于验证浮点数数组的文本输入字段.我的要求是限制用户输入字母等.
I am working with a angular and jquery based website. I have a text input field for validating array of floating numbers. My requirement is to restrict the user from entering alphabets, etc.
问题是我正在使用 e.preventDefault()
但它不能在 android 默认浏览器中工作,但在 android chrome 中完美工作.
The problem is I am using e.preventDefault()
but its not working in android default browser but working perfectly in android chrome.
我搜索了很多,但没有找到任何解决方案.
I have searched a lot but unable to get any solution.
我的示例代码:-
$('#test').keydown(function (e) {
e.stopPropagation();
e.preventDefault();
e.returnValue = false;
e.cancelBubble = true;
return false;
});
我也试过:-
$('#test').keydown(function (e) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
});
注意:- 我不能使用 keypress
事件,因为 android 默认浏览器不能监听这个事件.
Note:- I cannot use keypress
event as the android default browser cannot listen this event.
推荐答案
我在 Android 上的默认浏览器中也遇到了这个问题.preventDefault 没有帮助我,所以我使用了 .on jQuery 函数.
I also had this problem with default browser on Android. preventDefault didn't help me, so I used .on jQuery function.
$('#test').on('input',function () {
var inputText = $(this).val();
var resultText = inputText.replace(/[^0-9]/g, '');
$(this).val(resultText);
});
您可以将需要允许的其他字符添加到正则表达式.希望有帮助.
You can add to regex the other characters you need to be allowed. Hope it helps.
这篇关于Jquery preventDefault 不适用于 android 4.4 默认浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!