最近想到一个问题     就是在键盘给input(text)赋值之前能截取到 input的value 并且阻止后续操作么     
     上网查了些资料       发现控制字符串都是在  document.getElementById("xxx").value 生成之后才做得操作     怎么样才能在value生成之前截取到输入的值呢?

解决方案 »

  1.   

    keypress事件,校验传入的keycode是否符合要求,不符合,return false,符合则return true。
      

  2.   


    <!doctype html>
    <html>
    <head>
    <meta charset="gb2312" />
    <title></title>
    <style>
    </style>
    </head>
    <body>
    <input id="test" />
    <script>
    function $(o){return document.getElementById(o)}
    $('test').onkeydown = function(e){
    e = e || window.event;
    alert( e.keyCode )  //验证keyCode
    }
    </script>
    </body>
    </html>
      

  3.   

    onkeydown onkeyup onkeypress
      

  4.   

    其实是可以,按下一个键的时候触发keydown事件,这是还没给input赋值,但会有个keycode,判断这个keycode对应的是哪个键,就知道用户想输入的是什么东西了,如果不符合条件,直接return false,OK~
      

  5.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("test").onkeydown=function(event){
    return false;
    }
    document.getElementById("test2").onkeyup=function(event){
    return false;
    }
    document.getElementById("test3").onkeypress=function(){
    return false;

    }
    }
    </script>
    </head><body>
    <input type="text" id="test"  />
    <input type="text" id="test2"  />
    <input type="text" id="test3"  />
    </body>
    </html>
    刚才试了一下,keydown和keypress的时候是能截取的但是keyup事件是不行的,貌似已经生成了结贴了 ,   谢谢大家