var i = 0;
                function addAttachmentToList(){
                  if (findAttachment(event.srcElement.value)) return;
                  var span = document.createElement('span');
                  span.id = '_attachment' + i;
                  span.innerHTML = extractFileName(event.srcElement.value) + '&nbsp;<a href="javascript:delAttachment(' + (i++) + ')"><font color="blue">删除</font></a><br/>';
                  span.title = event.srcElement.value;
                  G('attachmentList').appendChild(span);                  if (G('attachmentList').style.display == 'none')
                  {
                    G('btnAdd').value = '继续添加';
                    G('attachmentList').style.display = '';
                    G('btnClear').style.display = '';
                  }                  G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个附件';
                }
                function selectAttachment()
                {

                  //需要多点一下保存
                   cleanInvalidUpfile();
                   var upfile  = '<input type="file"  onchange="addAttachmentToList();" name="_upfile'+i+'"  id="_upfile'+i+'">';
                   G('file').insertAdjacentHTML('beforeEnd', upfile);
                   G('_upfile'+i).click();

                //只点一下  但是后台获不到文件
               // cleanInvalidUpfile();
//var   upfile     =   document.createElement("INPUT");
//upfile.type = "file";
  // upfile.setAttribute("name",name);  //加了名称就需要多点一下  并且后台能保存
              //   upfile.name = "_upfile";//加了名称就需要多点一下  并且后台能保存
                //upfile.outerHTML   =   '<input type="file"  onchange="addAttachmentToList();" name="_upfile'+i+'"  id="_upfile'+i+'">';
                //G('file').appendChild(upfile);
        //upfile.click();

//
////style="display:none"
//                 // var upfile = '<input type="file"  onchange="addAttachmentToList();" name="_upfile'+i+'"  id="_upfile'+i+'">';
//                  var upfile = document.createElement("input");
//                   upfile.type = "file";
//                   upfile.name='_upfile';
//                   upfile.onchange='addAttachmentToList();';
//                   G('EditForm').appendChild(upfile);
//                  // G('EditForm').insertAdjacentHTML('beforeEnd', upfile);
//                    upfile.click();//
//  oFileInput.type   =   "file";
//  oFileInput.outerHTML   =   '<input type="file"  onchange="addAttachmentToList();" name="_upfile'+i+'"  id="_upfile'+i+'">';
// G('EditForm').appendChild(oFileInput);
//  oFileInput.click();
                }                function extractFileName(fn)
                {
                  return fn.substr(fn.lastIndexOf("\\") + 1);
                }                function findAttachment(fn)
                {
                  var o = G('attachmentList').getElementsByTagName('span');
                  for(var i=0;i<o.length;i++)
                  if (o[i].title == fn) return true;
                  return false;
                }                function delAttachment(id)
                {
                  G('attachmentList').removeChild(G('_attachment'+id));
                  G('file').removeChild(G('_upfile'+id));                  // 当附件列表为空则不显示并且变化添加附件按钮文本
                  if (G('attachmentList').childNodes.length == 0)
                  {                    G('btnAdd').value = '添加附件';
                    G('attachmentList').style.display = 'none';
                    G('btnClear').style.display = 'none';
                  }                  G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个附件';
                }                function cleanInvalidUpfile()
                {
                  var o = document.body.getElementsByTagName('input');
                  for(var i=o.length-1;i>=0;i--)
                  if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
                  {
                    if (!G('_attachment'+o[i].id.substr(7)))
                    G('file').removeChild(o[i]);
                  }
                }                function clearAttachment()
                {
                  var o = G('attachmentList').childNodes;
                  for(var i=o.length-1;i>=0;i--)
                  G('attachmentList').removeChild(o[i]);                  o = document.body.getElementsByTagName('input');
                  for(var i=o.length-1;i>=0;i--)
                  if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
                  {
                    G('file').removeChild(o[i]);
                  }                  G('btnAdd').value = '添加附件';
                  G('attachmentList').style.display = 'none';
                  G('btnClear').style.display = 'none';                  G('total').innerText = '当前选择上传0个附件';                }                function G(id)
                {
                  return document.getElementById(id);
                }
上面是JS
 <div id="file" >
                </div>
                     <input type="button" value="添加附件" id="btnAdd" onclick="selectAttachment();"/>  &nbsp;
                <input type="button" value="清空附件" id="btnClear" style="display:none" onclick="clearAttachment();"/>
                <div id="attachmentList" style="margin:3px 0px 0px 0px;padding:4px 3px 4px 3px;display:none;">
                </div>
这部分是HTML
我发现  我如果JS 调用   G('_upfile'+i).click();
  我提交的时候需要多点一下 
有没有高手    指点一下

解决方案 »

  1.   

    确保动态生成的INPUT对象放到指定的form对象内
      

  2.   

    结啦   找到其他方法啦   
    /*-------------------------------------------------------
    **num      为元素的下标
    **count    为元素的个数
    **name     为元素的名字和ID的前段(元素的实际名称是前段加下标)
    **status   为状态的ID
    **form     为表单的ID
    -------------------------------------------------------*/
    var File = {
    num : 1,
    count : 0,
    name : 'file',
    status : 'file',
    form : 'EditForm',
    urls : {},
    add : function (file) {
         //添加附件
            if (this.urls[file.value]) {
                alert('此文件已存在');
                return false;
            }
          var a = file.parentNode;
          var status = document.getElementById(this.status);
          this.urls[file.value] = 1;
          document.getElementById(this.form).appendChild(file);
          if (/Firefox/.test(window.navigator.userAgent)) {
            //这里一定要加,否则在FF中回出错
                var b = a, a = a.cloneNode(true);
                b.parentNode.replaceChild(a, b);
                b = null;
    }
    file.style.display = 'none';
    a.innerHTML =
    '<input id="' + this.name + (this.num + 1) + '" name="' + this.name + (this.num + 1) + '" onchange="File.add(this)" type="file" \/>';
    status.innerHTML += 
    ' <span>' + (/[^\\]+$/.exec(file.value)||'') + '<span class="del_file" onclick="File.del(this, ' + this.num + ')"><\/span>&nbsp;<\/span>'
    this.count ++, this.num ++, a = file= null;
    },
     del : function (span, num) {
         //删除附件
            var file = document.getElementById(this.name + num);
            delete this.urls[file.value];
            document.getElementById(this.form).removeChild(file);
            span.parentNode.parentNode.removeChild(span.parentNode);
            this.count --, span = num = null;
        } 
    };
    JS
        <style type="text/css">
    a.files, span.del_file {overflow:hidden;display:-moz-inline-box;display:inline-block;background:url(F2.gif);cursor:pointer;}
    a.files { width:70px;height:20px; #vertical-align:middle;cursor:pointer;background-image:url(/Collaboration/images/btn.gif);}
    a.files input {margin-left:-160px;filter:alpha(opacity=0);opacity:0; background-image:url(favicon.ico);cursor:pointer;}
    span.del_file {width:12px;height:12px;background-image:url(/Collaboration/images/delete.gif);}
    </style>
    css
      <a href="javascript:void(0);" class="files"><input id="file1" name="file1" onchange="File.add(this)" type="file" /></a>
                  <div id="file" class ="addfujian"></div>
    HTML
    希望大家有用
      

  3.   

     可以先写一个函数啊增中上传文件
    function AddFile()
    {
    var a="<input type="file" id="FIle2">";
    docuemnt.getElementByID("ControlID").insertAdjacentHTML('beforeEnd',a);
    }