如何将file_field_tag转换为链接?

时间:2012-12-26 08:44:34

标签: ruby-on-rails ruby-on-rails-3 file-upload

如何转换常用输入字段标记<%= file_field_tag :file, multiple: true %>,如normal input field tag

进入链接:

github

2 个答案:

答案 0 :(得分:2)

这对我有用:

HTML:

<input type='file' id='file' name='file' />
<a href="#" id='link-upload'>Upload</a>

JS:

<script type="text/javascript">
  $(function(){
    $('#link-upload').click(function(e){
      e.preventDefault();
      $('#file').click();
    });
  });
</script>

CSS:

#file{ width:0; height:0; } 

确保添加jQuery:

<script type="text/javascript">
  document.write("\<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'>\<\/script>");
</script>

答案 1 :(得分:0)

这对我有用:

HTML

<a class="btn-input">seleccionándolos<input type="file" multiple="" id="comment-images" /></a>

CSS

.btn-input {
  overflow: hidden;
  position: relative;
  cursor: pointer;
}
.btn-input input[type="file"] {
  height: 30px;
  cursor: pointer;
  position: absolute;
  top: 0px;
  right: 0px;
  z-index: 2;
  opacity: 0.0;
  /* Standard: FF gt 1.5, Opera, Safari */
  filter: alpha(opacity=0);
  /* IE lt 8 */
  -ms-filter: "alpha(opacity=0)";
  /* IE 8 */
  -khtml-opacity: 0.0;
  /* Safari 1.x */
  -moz-opacity: 0.0;
  /* FF lt 1.5, Netscape */ 
}

Example here