代码之家  ›  专栏  ›  技术社区  ›  Rohan

使用jQuery删除图像时参数不显示文件字段键

  •  0
  • Rohan  · 技术社区  · 7 年前

    我有一个需要删除上传图像的要求。

    目前,使用jQuery使用下面提到的代码删除图像;

    $(".image_remove").click(function(){
      $("#venue_pic").attr({src: null,
        alt: ""});
    });
    

    也用这个

    $(".image_remove").click(function(){
      $("#venue_pic").val('');  
    });
    

    但是params不会将文件字段属性显示为nil或“”(空字符串)。

    我知道使用carrierwave的remove_属性同样可以做到,但这将包括一个额外的ajax调用。

    但是我想通过params来管理它,这样如果文件字段标记中有任何更改,那么值就会相应地更新。

    因此,如何在rails中更新(删除)图像,以及为什么params没有在文件字段类型输入中检测到空值。

    1 回复  |  直到 6 年前
        1
  •  0
  •   kthatoto    7 年前

    你可以这么做

    $(function(){
      $(".deleteButton").on("click", function(){
        $newInput = $(".picture").clone()
        $(".picture").remove()
        $(".pictureWrapper").append(
          $("<input type='file'>").addClass($newInput.attr("class")).attr("name", $newInput.attr("name"))
          //or
          //$("<input type='file' name='picture' class='picture'>")
        )
      })
    })
    .deleteButton {
      display: inline-block;
      border: 1px solid black;
      cursor: pointer;
      padding: 5px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="pictureWrapper">
      <input type="file" name="picture" class="picture">
    </div>
    <div class="deleteButton">X</div>
    推荐文章