多文件上传

发布于 2024-12-03 09:34:31 字数 483 浏览 3 评论 0原文

<?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 风格的文件上传,您可以在其中选择文件对于第一个字段,然后是下一个弹出窗口...

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

<?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 } ?>

I am able to create three file upload fields, but i want to add a new input file filed only when i select a file.... it should keep on increasing.... something like Facebook style file upload where you select a file for the first field, then the next popup's...

I am actually checking for click event of browse, but it does not work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-12-10 09:34:31

这是一个非常基本的纯 JS 实现 - 希望它能给你一个正确方向的推动......

<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>

Here is a very basic pure-JS implementation - hopefully it will give you a nudge in the right direction...

<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>
霞映澄塘 2024-12-10 09:34:31

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

我认为这适合您的情况。

Try hooking some other events to your file field. As far as I know, jQuery has a very useful event called .change()

I think that would work in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文