<html>
<head>
<script>
function handleKeyPress(evt) {
var nbr;
var nbr = (window.event) ? event.keyCode : evt.which;
alert(nbr);
return true;
}
</script>
</head>
<body>
<form>
<input type=text name="txt" onkeypress="handleKeyPress();">
</form>
</body>
</html>
这与IE完美配合,但在Firefox中没有。我正在使用IE8和Firefox 3
答案 0 :(得分:4)
您错过了将event
对象传递给handleKeyPress()
来电。
<input type="text" name="txt" onkeypress="handleKeyPress(event);">
答案 1 :(得分:1)
试试这个
<script type="text/javascript">
function onlyNumbers(evt)
{
var nbr;
var nbr = (window.event) ? event.keyCode : evt.which;
if ((nbr >= 48 && nbr <= 57) || nbr == 8)
{
return true;
}
else
{
return false;
}
}
</script>
html是
onkeypress =“return onlyNumbers(event);”
答案 2 :(得分:0)
function getFilter(e, obj){
e=e||event;
console.log(e.ctrlKey);
if (e.keyCode===10 || (e.ctrlKey && e.keyCode===13)){
//works on chrome + firefox filter for ctl+enter
}
}