<input type="text" id="txt" onkeydown="show(obj);"/>
<script type="text/javascript">
        function show(obj)
        {
            alert(obj);    
           
        }
    </script>这是我写的,但是无法获得文本框的值,那我该怎么做才能取得文本框的值呢,当然必须是键盘按下事件取值

解决方案 »

  1.   

    总感觉这样的实现方式不好<input type="text" id="txt" onkeydown="show(this);"/>
    <script type="text/javascript">
    function show(obj){
      alert(obj.value);  
    }
    </script>
      

  2.   


    <input type="text" id="txt" onkeyup="show(this);"/>
    <script type="text/javascript">
      function show(obj)
      {
    alert(obj.value);      
      }
    </script>
      

  3.   


    <input type="text" id="txt" onkeyup="show(this);"/>
    <script type="text/javascript">
      function show(temp)
      {
        alert(temp.value);      
      }
    </script>
    一般取值都是到keyup的时候才取,因为按键输入了,肯定要等到输入完再取值的
      

  4.   


    <input type="text" id="txt" onkeydown="show(this);"/>
    <!-- onkeydown 触发的时候所按的key的值还没有写到input里面,
    如果要包含按下的键的值,可以试试onkeyup -->
    <script type="text/javascript">
      function show(obj)
      {
      alert(obj.value);   
      }
      </script>
      

  5.   

    楼上正解。
    按键按下的动作发生的时候,value值还是空的,要按键弹起的时候value才有值
      

  6.   

    用keyup,才能及时获得按键之后的值:
    <html>
    <head >
      <title>Test</title>
      <meta HTTP-EQUIV ="Content-Type" content="text/html;charset=utf-8">
      <style type="text/css">  </style></head>
    <body onload ="a = 1">
    <input type="text" id="txt" onkeyup="show(this.value);"/>
    <div id="outPut"></div>
    <script type="text/javascript">
      function show(str)
      {
    document.getElementById('outPut').innerHTML =str;  
      }
     </script>
    </body>
    </html>