douchen9855 2017-05-20 11:52
浏览 54
已采纳

上传图片后如何移动按钮?

My html code is like this :

<input type='file' multiple style="display: none;" id="upload-file" />
<div class="img-container">
    <button type="submit" class="btn btn-primary" id="upload-add-product">
        <i class="glyphicon glyphicon-plus"></i>
    </button>
</div>
<?php
    for($i=0;$i<4; $i++) {

?>
    <div class="img-container" id="box<?php echo $i ?>">
    </div>
<?php
    }
?>

My javascript code is like this :

    $("#upload-add-product").click(function(){
        $("#upload-file").click();
    });

    $(function () {
        $(":file").change(function () {
            var noOfFiles = this.files.length;
            for(var i=0; i < noOfFiles; i++) {        
                var reader = new FileReader();
                reader.onload = imageIsLoaded;
                reader.readAsDataURL(this.files[i]);
            }        
        });
    });

    function imageIsLoaded(e) {
        var imgTmpl = '<img height="142" width="162" src='+e.target.result+'>';
        var IsImgAdded=false;
        $('.img-container').each(function(){
            if($(this).find('img').length==0 && IsImgAdded==false){
                $(this).append(imgTmpl);
                IsImgAdded=true;
            }
        });     
    };

Demo and full code is like this : http://phpfiddle.org/main/code/q47z-p15c

I want to make like this :

  • When user select 2 image, plus icon will move to box number 3

  • When user selects 5 image, plus icon will disappear

I had try to move the plus icon, but I can't do it

How can I solve this problem?

  • 写回答

2条回答 默认 最新

  • drr25281 2017-05-20 14:42
    关注

    You can add buttons to each container only the first button will be visible initially. display the next button only after the image is added to the container

    Update your Html as given below :

    <?php   for($i=0;$i<5; $i++) { ?>
    
      <div class="img-container" id="box<?php echo $i ?>" data-status="0">
        <button type="submit" class="btn btn-primary upload-add-product"<?php 
      if($i!=0) echo' style="display:none;"'; ?> >
        <i class="glyphicon glyphicon-plus"></i>
      </button>
      </div>
    
    <?php } ?>
    

    Update the JS Function imageIsLoaded() after you set the IsImgAdded flag

     $(this).attr('data-status',1)
     $(this).find('button').hide()
     $('.img-container').each(function(){
    
       if( $(this).attr('data-status') != 1){
         $(this).find('button').show();
         return false;
       }
    
    })
    

    PS : i changed the id : upload-add-product to class upload-add-product You might need to tweak the code..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算