把:alert(j);去掉试试alert后file控件可能失去focus

解决方案 »

  1.   

    原因是因为focus()的速度太快,sendKey来不及响应,
    所以都跑到最后一个里面去了,可以设置一个短小的延迟来解决<!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=gb2312">
    <title>无标题文档</title>
    </head>
    <script language="JavaScript" type="text/JavaScript">
    function getfiles(){
    var arr = new Array();
    arr[0]=0;
    arr[1]=1;
    arr[2]=2;
    var WshShell=new ActiveXObject("WScript.Shell");
    for(j=0;j<3;j++){
    var f = function(i){
    //alert(i);
     form1.elements['file'+i].focus();
     WshShell.sendKeys(arr[i]);
     }
      setTimeout(f.bind(window,j), 50*(j+1));
    }
    }
    Function.prototype.bind = function(o){
    var self = this;
    var arg  = Array.prototype.slice.call(arguments,1);
    return function(){
    self.apply(o,arg);
    }
    }
    </script>
    <body>
    <p><a href="#" onClick="getfiles();" >asdfasf</a>
    </p>
    <form name="form1" enctype="multipart/form-data" method="post" action="">
      <input type="file" name="file0" >
      <input type="file" name="file1">
      <input type="file" name="file2">
    </form>
    <p>&nbsp; </p>
    </body>
    </html>