Javascript HTML将多个文件添加到输入中

Javascript HTML将多个文件添加到输入中,javascript,html,Javascript,Html,我知道这很简单,我只需要在文件输入中添加多行。但是 我正在使用 这个脚本我也知道有一个新的,但我想用这个,还有像dropzone.js这样的脚本,但我想不出我在这里问了些什么,但没有人回答 所以我选择了这个剧本。 我将其添加到dropzone表单中,如下所示: <div style="margin-bottom: 1em;"> <div class="file-well">

我知道这很简单,我只需要在文件输入中添加多行。但是 我正在使用 这个脚本我也知道有一个新的,但我想用这个,还有像dropzone.js这样的脚本,但我想不出我在这里问了些什么,但没有人回答

所以我选择了这个剧本。 我将其添加到dropzone表单中,如下所示:

       <div style="margin-bottom: 1em;">
                <div class="file-well">                
                    <input type="file" name="file" id="my_file_element" multiple/>
                    <span id="filewelllabel">click here to add file or drag and drop<br/></span>
                    <span id="filewelllabel">up to 5 files</span>
                </div>
            </div>
            <div id="files_list"></div>
所有的工作,因为我希望它的工作,但最大的问题是,我只能添加一个文件一次。即使我选择更多,或者拖放更多,也只添加一个。 谁能帮我修改代码,这样我就可以一次只放几个文件,而不是一个接一个地放

@@@@@ 如果您不想下载,以下是脚本:

/**
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   5. That's it.
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 *   You might also want to change the line 
 *       element.name = 'file_' + this.count;
 *   ...to a naming convention that makes more sense to you.
 * 
 * Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 * Credit:
 *   If you're nice, you'll leave this bit:
 *  
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
function MultiSelector( list_target, max ){

    // Where to write the list
    this.list_target = list_target;
    // How many elements?
    this.count = 0;
    // How many elements?
    this.id = 0;
    // Is there a maximum?
    if( max ){
        this.max = max;
    } else {
        this.max = -1;
    };

    /**
     * Add a new file input element
     */
    this.addElement = function( element ){

        // Make sure it's a file input element
        if( element.tagName == 'INPUT' && element.type == 'file' ){

            // Element name -- what number am I?
            element.name = 'file_' + this.id++;

            // Add reference to this object
            element.multi_selector = this;

            // What to do when a file is selected
            element.onchange = function(){

                // New file input
                var new_element = document.createElement( 'input' );
                new_element.type = 'file';

                // Add new element
                this.parentNode.insertBefore( new_element, this );

                // Apply 'update' to element
                this.multi_selector.addElement( new_element );

                // Update list
                this.multi_selector.addListRow( this );

                // Hide this: we can't use display:none because Safari doesn't like it
                this.style.position = 'absolute';
                this.style.left = '-1000px';

            };
            // If we've reached maximum number, disable input element
            if( this.max != -1 && this.count >= this.max ){
                element.disabled = true;
            };

            // File element counter
            this.count++;
            // Most recent element
            this.current_element = element;

        } else {
            // This can only be applied to file input elements!
            alert( 'Error: not a file input element' );
        };

    };

    /**
     * Add a new row to the list of files
     */
    this.addListRow = function( element ){

        // Row div
        var new_row = document.createElement( 'div' );

        // Delete button
        var new_row_button = document.createElement( 'input' );
        new_row_button.type = 'button';
        new_row_button.value = 'Delete';

        // References
        new_row.element = element;

        // Delete function
        new_row_button.onclick= function(){

            // Remove element from form
            this.parentNode.element.parentNode.removeChild( this.parentNode.element );

            // Remove this row from the list
            this.parentNode.parentNode.removeChild( this.parentNode );

            // Decrement counter
            this.parentNode.element.multi_selector.count--;

            // Re-enable input element (if it's disabled)
            this.parentNode.element.multi_selector.current_element.disabled = false;

            // Appease Safari
            //    without it Safari wants to reload the browser window
            //    which nixes your already queued uploads
            return false;
        };

        // Set row value
        new_row.innerHTML = element.value;

        // Add button
        new_row.appendChild( new_row_button );

        // Add it to the list
        this.list_target.appendChild( new_row );

    };

};
@@@@@ 这是html文件中的脚本:

 <script>
    <%-- Create an instance of the multiSelector class, pass it the output target and the max number of files --%>
    var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 5 );
    <%-- Pass in the file element --%>
    multi_selector.addElement( document.getElementById( 'my_file_element' ) );
</script>
@编辑

这是我现在得到的


这里是我得到的工作示例

也许可以使用codepen或JSFIDLE设置一个工作示例以获得更多响应我可以拖放多达5个文件,一次一个。你的问题到底是什么?问题是,当你这样做时-只添加了一个-尝试拖放5个,一个接一个,你会在表单下看到5个文件名,然后按X按钮删除每个文件名。如果您同时dnd 5,您将只看到一个。我想让他们都知道你有什么想法吗?