首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多文件上传

多文件上传
EN

Stack Overflow用户
提问于 2011-09-07 05:29:24
回答 2查看 174关注 0票数 0
代码语言:javascript
复制
<?php $uploadNeed=3 ; for($i=0;$i<$uploadNeed;$i++) { ?>
    <div class="smWidth">
        <input type="file" name="upload_file" value="" id=" " OnClick="alert("
        2 ");" />
        <br />
        <div class="clear">
        </div>
    </div>
    <?php } ?>

我可以创建三个文件上传域,但我希望仅当我选择一个文件时才添加一个新的输入文件。它应该继续增长……类似Facebook风格的文件上传,你可以在第一个字段中选择一个文件,然后在下一个弹出窗口中...

我实际上正在检查浏览的点击事件,但它不工作...

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-07 05:57:21

这是一个非常基本的纯JS实现--希望它能推动你朝着正确的方向前进……

代码语言:javascript
复制
<script type="text/javascript">

  // This keeps track of how many inputs there are, because each one MUST have a unique name!
  var inputCounter = 1;

  function inputChange() {

    // This function will add a new input element, in a div, as it is in your
    // original code, every time it is called. We attach it to the onchange
    // event of all the inputs

    // Declare local variables and increment input counter (for input names)
    var newContainerDiv, newClearDiv, newInput, newBr;
    inputCounter++;

    // Create the new elements and set their properties
    newContainerDiv = document.createElement('div');
    newContainerDiv.className = 'smWidth';
    newInput = document.createElement('input');
    newInput.type = 'file';
    newInput.name = 'upload_file_'+inputCounter;
    newInput.onchange = inputChange;
    newBr = document.createElement('br');
    newClearDiv = document.createElement('div');
    newClearDiv.className = 'clear';

    // Add the new elements to the DOM
    newContainerDiv.appendChild(newInput);
    newContainerDiv.appendChild(newBr);
    newContainerDiv.appendChild(newClearDiv);
    document.getElementById('uploads_container').appendChild(newContainerDiv);

  }

</script>

<!-- just create one input at first, so we don't need the PHP loop any more -->

<div id="uploads_container">
  <!-- we wrap it all in an outer container div with an id, to ease the task of adding more elements -->
  <div class="smWidth">
    <input type="file" name="upload_file_1" onchange="inputChange();" />
    <br />
    <div class="clear">
    </div>
  </div>
</div>
票数 1
EN

Stack Overflow用户

发布于 2011-09-07 05:31:55

尝试将一些其他事件挂钩到您的文件字段。据我所知,jQuery有一个非常有用的事件,叫做.change()

我想这在你的情况下是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7326401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档