比如有一个文本框
<input type="text" onkeydown="click(this)" /><script>
function click(obj) {
alert(obj.value);
}
</script>在使用onkeydown事件的时候,第一次输入的值不被显示
只有第二次的才会显示,为什么会这样呢?难道onkeydown
是记录两次键盘被按下才触发的吗?
请问怎么使用onkeydown来实现第一次按下就触发事件
不使用onkeyup或者onkeypress
<input type="text" onkeydown="click(this)" /><script>
function click(obj) {
alert(obj.value);
}
</script>在使用onkeydown事件的时候,第一次输入的值不被显示
只有第二次的才会显示,为什么会这样呢?难道onkeydown
是记录两次键盘被按下才触发的吗?
请问怎么使用onkeydown来实现第一次按下就触发事件
不使用onkeyup或者onkeypress
LZ不妨试试onkeyup就不存在这个问题,onkeyup是按键弹起时触发的。这时按键输入系统已经返回。
keydown获得值是前一次输入的值,是键盘按下的时候触发。只有键盘松开的时候
才能获取当前输入的值,建议用keyup、keypress; lz就不要纠结在这个问题上了
而且ls解释都比较好
function clickDown(obj,evt) {
alert("文本框值:"+obj.value);
evt = (evt) ? evt : window.event
alert("keyCode:"+evt.keyCode);
alert("keyCode对应的字符:"+String.fromCharCode(evt.keyCode+32));
}以click作为函数名,很奇怪,我机器不认为是个函数~~不知道楼主怎么运行的
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript"> var isIE = /msie/i.test(navigator.userAgent) && !window.opera; window.onload = function() {
var inputTest = document.getElementById('inputTest');
if (isIE) {
inputTest.onpropertychange = valueChangeHandler;
} else {
if (inputTest.watch) {
inputTest.watch("value", function(id, oldVal, newVal) {
valueChangeHandler();
})
} else {
inputTest.oninput = valueChangeHandler;
}
}
}; function valueChangeHandler() {
var inputTest = document.getElementById('inputTest');
var consoleDiv = document.getElementById('console');
var messageDiv = document.createElement('div');
messageDiv.innerHTML = inputTest.value;
consoleDiv.appendChild(messageDiv);
}</script>
</head>
<body>
<input id="inputTest" type="text">
<div id="console"></div>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript"> var isIE = /msie/i.test(navigator.userAgent) && !window.opera; window.onload = function() {
var inputTest = document.getElementById('inputTest');
if (isIE) {
inputTest.attachEvent('onpropertychange', valueChangeHandler);
} else {
// Firefox下,只有DOM第2级事件才有效,DOM第0级无效
inputTest.addEventListener('input', function() {
valueChangeHandler();
}, false);
}
}; function valueChangeHandler() {
var inputTest = document.getElementById('inputTest');
var consoleDiv = document.getElementById('console');
var messageDiv = document.createElement('div');
messageDiv.innerHTML = inputTest.value;
consoleDiv.appendChild(messageDiv);
}</script>
</head>
<body>
<input id="inputTest" type="text">
<div id="console"></div>
</body>
</html>再度狂汗。