2016-02-29 163 views
2

也许我只是愚蠢这里,但是当我点击“新增”,它只是加载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

你的代码是好的。 jquery库在哪里? –

+1

我的意思是在这段代码中没有错误。我知道一些浏览器将默认按钮视为提交。但他使用的是防止默认值。所以他的代码中的一切都很好,除了jQuery库。 –

+1

是的。你是对的。我认为父文件包含了它,但我错了。这是一个学校男孩的错误! –

回答

3

您的代码工作正常,你应该只需要添加的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>

+0

我很蠢,为什么e.preventDefault()不起作用? –

+1

你不傻@BhojendraNepal - 就是我。学校的男孩错误,我认为父文件包含库。我的错! –