有人知道在javascript中如何取得键盘输入的字符吗?

来源:百度知道 编辑:UC知道 时间:2024/05/22 06:38:38
有人知道在javascript中如何取得键盘输入的字符吗?
不要ACS码,再转回字符会有误差。
或能提供一个不错的控制textbox,只能输入double型的方法(字母等其它字符输入不到textbox中),谢谢了。只想通过客服端实现,不用去服务端。

<script>
    document.onkeypress=function(ev){
    var oEvent = ev || event;   //处理兼容
    alert(String.fromCharCode(oEvent.keyCode)); 
    };
</script>
这段代码你可以直接放到console里运行。你在页面中按啥出啥。

<html>
<body>
<script language="javascript">
var r={
'special':/[\W]/g,
'quotes':/['\''&'\"']/g,
'notnumbers':/[^\d|\.]/g
}

function valid(o,w){
o.value = o.value.replace(r[w],'');
}
</script>
<form id="aform" name="aform">
<input name="inputint" id="inputint" type="text" onKeyUp="valid(this,'notnumbers')">
</form>
</body>