添加新文件上载字段

时间:2016-02-29 09:17:07

标签: javascript html

也许我只是在这里愚蠢,但当我点击“添加新”时,它只是加载upload.php

它没有添加其他输入

<html>
  <head>
    <script>
      $(document).ready(function(){
        $('.add_more').click(function(e){
          e.preventDefault();
          $(this).before("<input name='file[]' type='file'/>");
        });
      });
    </script>
  </head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
      <input name="file[]" type="file" />
      <button class="add_more">Add More Files</button>
      <input type="submit" value="Upload File" id="upload"/>
    </form>
  </body>
</html>

感谢任何可以告诉我在哪里滑倒的人。

2 个答案:

答案 0 :(得分:4)

你的代码工作得很好你应该只是添加jQuery lib,如下所示,e.preventDefault()将阻止按钮提交表单:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

希望这有帮助。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    <script>
      $(document).ready(function(){
        $('.add_more').click(function(e){
          e.preventDefault();
          $(this).before("<input name='file[]' type='file'/>");
        });
      });
    </script>
  </head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
      <input name="file[]" type="file" />
      <button type='button' class="add_more">Add More Files</button>
      <input type="submit" value="Upload File" id="upload"/>
    </form>
  </body>
</html>

答案 1 :(得分:-1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    <script>
      $(document).ready(function(){
        $('.add_more').click(function(e){
          e.preventDefault();
          $(this).before("<input name='file[]' type='file'/>");
        });
      });
    </script>
  </head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
      <input name="file[]" type="file" />
      <button type='button' class="add_more">Add More Files</button>
      <input type="submit" value="Upload File" id="upload"/>
    </form>
  </body>
</html>