比如有一个文本框
<input type="text" onkeydown="click(this)" /><script>
function  click(obj) {
    alert(obj.value);
}
</script>在使用onkeydown事件的时候,第一次输入的值不被显示
只有第二次的才会显示,为什么会这样呢?难道onkeydown
是记录两次键盘被按下才触发的吗?
请问怎么使用onkeydown来实现第一次按下就触发事件
不使用onkeyup或者onkeypress

解决方案 »

  1.   

    onkeydown 是键盘按键按下时触发,按键按下时,系统硬件还没响应按键的输入返回。所以第一次就没有看到输入值。
    LZ不妨试试onkeyup就不存在这个问题,onkeyup是按键弹起时触发的。这时按键输入系统已经返回。
      

  2.   

    也是一样的. 当判断值为空的时候 不向后台请求就可以了. 用onkeydown没问题. 
      

  3.   


    keydown获得值是前一次输入的值,是键盘按下的时候触发。只有键盘松开的时候
    才能获取当前输入的值,建议用keyup、keypress; lz就不要纠结在这个问题上了
    而且ls解释都比较好
      

  4.   

    键盘down下去的时候,值还没有进入文本框,所以取不到值。你可以直接获取键盘按了什么键,来获得按键值
    function clickDown(obj,evt) {
      alert("文本框值:"+obj.value);
      evt = (evt) ? evt : window.event
      alert("keyCode:"+evt.keyCode);
      alert("keyCode对应的字符:"+String.fromCharCode(evt.keyCode+32));
    }以click作为函数名,很奇怪,我机器不认为是个函数~~不知道楼主怎么运行的
      

  5.   

    onkeydown事件不同的浏览器,效果不一样的。我这里只有第一下是触发的,其他的都不触发,狂汗!!每次输入值的时候触发,可以用这个
    <!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>
      

  6.   

    有问题的,我在做类似google suggest的功能,第一个字母出来了,而suggest没出来就不对了
      

  7.   

    <!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.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>再度狂汗。