javascript - 使用 droparea js 在 1 个页面中创建多个上传图像

标签 javascript jquery html css

我正在使用 droparea js。我有4个上传。问题是如果我想在其中一个上传菜单中上传图片,它会更改所有 4 个上传菜单。

    <div class="col-md-12">
                    <div class="droparea" >
                    <img src="http://placehold.it/200" class="file_preview" >
                </div>
                <input type="file" name="file" id="file" accept="image/*" style="display: none;" >
    <div class="col-md-12">
                    <div class="droparea" >
                    <img src="http://placehold.it/200" class="file_preview" >
                </div>
                <input type="file" name="file" id="file" accept="image/*" style="display: none;" >
    <div class="col-md-12">
                    <div class="droparea" >
                    <img src="http://placehold.it/200" class="file_preview" >
                </div>
                <input type="file" name="file" id="file" accept="image/*" style="display: none;" >
    <div class="col-md-12">
                    <div class="droparea" >
                    <img src="http://placehold.it/200" class="file_preview" >
                </div>
                <input type="file" name="file" id="file" accept="image/*" style="display: none;" >
<script src="js/droparea.js"></script>
<script>
      $(document).ready(function(){
    $('.droparea').droparea({
                url: 'server.php',
                success: function( server_return, name, uploaded_file )
                {
                    $('.droparea').after( $('<p />').html( 'File sent: <b>' + name + '</b>' ) );

                    var oFReader = new FileReader();

                    oFReader.readAsDataURL( uploaded_file );
                    oFReader.onload = function (oFREvent)
                    {
                        $( '.file_preview' ).animate({opacity: 0}, 'slow', function(){
                            // change the image source
                            $(this)
                                .attr('src', oFREvent.target.result).animate({opacity: 1}, 'fast')
                                .on('load', function()
                                {
                                    $('.statusbar').css({
                                        width: $('.droparea').outerWidth(),
                                        height: $('.droparea').outerHeight()
                                    });
                                });

                            // remove the alert block whenever it exists.
                            $('.droparea').find('.statusbar.alert-block').fadeOut('slow', function(){ $(this).remove(); });
                        });
                    };
                }
            });
  });


  </script>

我想放在jsfiddle上,但是我无法提交droparea插件的在线js和css。

如果我第一次在第二个菜单或除第一个菜单以外的任何人上传,结果将显示在第一个菜单中。所以我从 id="file-preview" 更改为 class="file-preview" 它显示给所有人。你能教我如何让它可以将 1 个之间的不同图像上传到另一个吗?

最佳答案

我不熟悉 droparea,但您可能需要为每个实例创建一个单独的实例,而不是一个单独的 .droparea() 实例,例如;

$('.droparea').each(function(){
   $(this).droparea()
});

你可能需要这个:

$('.droparea').each(function(){
 $(this).droparea({
            url: 'server.php',
            success: function( server_return, name, uploaded_file )
            {
                $('.droparea').after( $('<p />').html( 'File sent: <b>' + name + '</b>' ) );

                var oFReader = new FileReader();

                oFReader.readAsDataURL( uploaded_file );
                oFReader.onload = function (oFREvent)
                {
                    $( '.file_preview' ).animate({opacity: 0}, 'slow', function(){
                        // change the image source
                        $(this)
                            .attr('src', oFREvent.target.result).animate({opacity: 1}, 'fast')
                            .on('load', function()
                            {
                                $('.statusbar').css({
                                    width: $('.droparea').outerWidth(),
                                    height: $('.droparea').outerHeight()
                                });
                            });

                        // remove the alert block whenever it exists.
                        $('.droparea').find('.statusbar.alert-block').fadeOut('slow', function(){ $(this).remove(); });
                    });
                };
            }
        })
   })

关于javascript - 使用 droparea js 在 1 个页面中创建多个上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404953/

相关文章:

javascript - 如何在js中连接字符串和数字

javascript - 如何正确清除 ionic 历史记录?

javascript - 中间显示 "x"的神秘非常小的矩形框,但无法位于 DOM 上的任何位置

php - 没有显示需要显示的内容

javascript - 按值复制数组

javascript - 没有背景图像的视差 div

javascript - iframe src 添加 autoplay=true 参数 onclick 和现有值

javascript - 当事件设置为外部元素时,如何检测另一个元素内部的元素?

javascript - 在 AngularJS 中,HTML 模板中包含的任何内联 javascript 代码都不起作用

javascript - 您有任何与 jQuery 一起使用的 Ajax 加载图像的想法吗?