jquery只能输入数字

<html>
<head>
<title>1st</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
<script src="js/jquery-1.8.3.js" type="text/javascript">
/**window.onload=function(){
document.getElementById("1st").innerHTML="helloWorld";
}**/
</script>
// imeMode有四种形式,分别是:
// active 代表输入法为中文
// inactive 代表输入法为英文
// auto 代表打开输入法 (默认)
// disable 代表关闭输入法
//同事收藏的做法:
//1.正则表达式限制Float:(^[0-9]([.][0-9]{1,2})?$)|(^1[0-9]([.][0-9]{1,2})?$)|(^2[0-3]([.][0-9]{1,2})?$)|(^24([.]0{1,2})?$)
//2.限制输入
//function KeyPress(objTR) {//只允许录入数据字符 0-9 和小数点
// //var objTR = element.document.activeElement;
// var txtval = objTR.value;
// var key = event.keyCode;
// if ((key < 48 || key > 57) && key != 46) {
// event.keyCode = 0;
// alert("只能输入数字");
// }
// else {
// if (key == 46) {
// if (txtval.indexOf(".") != -1 || txtval.length == 0)
// event.keyCode = 0;
// }
// }
// }
//3.禁止切换输入法
//Style: "ime-mode: disabled"
<script type="text/javascript">
$(function(){
$("#hjg").keydown(function(e){   
    // 注意此处不要用keypress方法,否则不能禁用Ctrl+C与Ctrl+V,具体原因请自行查找keyPress与keyDown区分,十分重要,请细查
if ($.browser.msie) { // 判断浏览器
if ( ((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 8) ) {  // 判断键值
return true;
} else {
return false;
}
} else {
if ( ((e.which > 47) && (e.which < 58)) || (e.which == 8) || (event.keyCode == 17) ) {
return true;
} else {
return false;
}
}}).focus(function() {
this.style.imeMode='disabled'; // 禁用输入法,禁止输入中文字符

   });
});

</script>
<style type="text/css">

</style>
</head>
<body>
<input type="text" id="hjg" style="height:50px;weight:250px">
</body>
</html>

posted @ 2016-04-07 21:01  光明小将军  阅读(370)  评论(0编辑  收藏  举报