我想实现点击浏览后,再增加一个文件域,但是用jquery怎么好像实现不了,请大家帮忙看下,谢谢
html:
<form action="" method="post" enctype="multipart/form-data" name="form1" id="upfilebox">
  <label>
  文件上传:
  <input type="file" name="file0" id="att_0"/>
  </label>
</form>
js:
<script type="text/javascript">
$(document).ready(function(){
id=0;
$("#att_"+id).change(
function(){
$("#upfilebox").append('<input id="att_' + (id + 1) + '" name="infoimg[]" type="file" class="input" size="40">');
i++;
}
)
})
</script>

解决方案 »

  1.   

    <div id="upfilebox">
      文件上传:
      <input type="file" name="file0" id="att_0"/>
      </div><script type="text/javascript">
    $(document).ready(function(){
    id=0;
    $("#att_"+id).change(
    addFile();
    i++;
    }
    )
    });
    function addFile(){
    $("#upfilebox").append('<input id="att_' + (id + 1) + '" name="infoimg[]" type="file" class="input" size="40" onchange="addFile()">');i++;}
    </script>
      

  2.   

    你这样写只会在页面加载完成的时候给att_0这个input加上onchange事件,动态生成的input是不会有onchange事件的,你应该写一个事件来实现动态添加,如下:<body>
        <form action="" method="post" enctype="multipart/form-data" name="form1"
        id="upfilebox">
            <label>
                文件上传:
                <input type="file" name="file0" id="att_0" onchange="OnChange()" />
            </label>
        </form>
    </body>
    <script>
        var id = 0;    function OnChange() {
            $("#upfilebox").append('<input onchange="OnChange()" id="att_' + (id + 1) + '" name="infoimg[]" type="file" class="input" size="40">');
            id++;
        }
    </script>
      

  3.   

    楼上正解
    $(document).ready(function(){}里的事件是在文挡加载时发生,你后加的input这时还未生成所以不会有onchange事件
      

  4.   

    <script src="http://code.jquery.com/jquery-latest.min.js"></script><form action="" method="post" enctype="multipart/form-data" name="form1" id="upfilebox">
      <label>
      文件上传:<br />
      <input type="file" name="file0" id="att_0" />
      </label>
    </form><script type="text/javascript">
    $(document).ready(function() {
    id = 0;
    $("#att_" + id).change(function() {
    theforeverAddOneFile();
    })
    });function theforeverAddOneFile() {
    $("#upfilebox").append('<br /><input id="att_' + (id + 1) + '" name="infoimg[]" type="file" class="input" size="40" onchange="theforeverAddOneFile()">');
    id++;
    }
    </script>