html代码:
<input type="text" style='width:20px;'  maxlength="2"/>
<input type="text" style='width:20px;'  maxlength="2"/>
 
请问如果在第一个input中粘贴一个长度为4的数字后,立即自动触发一个函数事件,是长度为4的数字分别显示在两个input中

解决方案 »

  1.   

    onbeforepaste  这个事件可以
      

  2.   


    <html>
    <head></head>
    <script type="text/javascript">
    function test(event)
    {
    //window.event.returnValue=false;
    var str = clipboardData.getData("Text");
    document.getElementById("text2").value = str.substr(2,2);

    }
    </script>
    <body>
    <input type="text" id="text1" style='width:20px;' maxlength="2" onbeforepaste="test();"/>
    <input type="text" id="text2" style='width:20px;' maxlength="2"/>
    </body>
    </html>
      

  3.   

    <script type="text/javascript">
    function test()
    {
        //window.event.returnValue=false;
    var t=document.getElementById("text1").value;
    if(t.length>2){
        document.getElementById("text2").value = t.substr(2,2);
    document.getElementById("text1").value=t.substr(0,2);
    }
    }
    </script>
    <body>
        <input type="text" id="text1" style='width:100px;' maxlength="4" onChange ="test();"/>
        <input type="text" id="text2" style='width:100px;' maxlength="2"/>
    </body>
      

  4.   

    如果你想要自己一个一个的输入的话就是
    <script type="text/javascript">
    function test()
    {
        //window.event.returnValue=false;
    var t=document.getElementById("text1").value;
    if(t.length>2){
        document.getElementById("text2").value = t.substr(1,2);
    document.getElementById("text1").value=t.substr(0,1);
    }
    }
    </script>
    <body>
        <input type="text" id="text1" style='width:100px;' maxlength="4" onKeyPress="test();"/>
        <input type="text" id="text2" style='width:100px;' maxlength="2"/>
    </body>
      

  5.   

    如果楼主仅需支持IE的话,使用下面的代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        
    <script type="text/javascript">
    function setMyPaste(input) {
    var value = input.value;
    document.getElementById("receive1").value = value.substr(0,2);
    document.getElementById("receive2").value = value.substr(2,2);
    }
    </script>
      </head>
      
      <body>
        <table border="1">
         <tr>
         <td>粘贴输入框</td>
         <td>
         <input onpropertychange="setMyPaste(this)"/>
         </td>
         </tr>
         <tr>
         <td>接收输入框1</td>
         <td>
         <input id="receive1" />
         </td>
         </tr>
         <tr>
         <td>接收输入框2</td>
         <td>
         <input id="receive2"/>
         </td>
         </tr>
        </table>
      </body>
    </html>
      

  6.   

    就像填写Adobe的注册码那样,在其他的地方复制了注册码,在填写注册码的第一格粘贴后,自动填充到其他格
      

  7.   

    用onpropertychange
    <!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=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function splitNum(){
       var strNum=window.clipboardData.getData("Text");
       var strNum2,len;
       strNum2="";
       len=strNum.length;
       if(len>2){
       strNum2=strNum.substring(2);  
       }
       document.getElementById("numb2").value = strNum2; 
    }
    </script>
    </head><body>
    <input type="text" style='width:20px;' maxlength="2" id="numb1" onpropertychange="splitNum();"/>
    <input type="text" style='width:20px;' maxlength="2" id="numb2"/>
    </body>
    </html>
      

  8.   

    如果楼主要支持 跨浏览器的话可以使用setTimeout + onpaste来完成,我测试成功了的。