简体   繁体   English

选择图像时显示预览div

[英]Show a preview div when image is selected

I have a image preview div for displaying the selected image's thumbnail. 我有一个图像预览div,用于显示所选图像的缩略图。 It works fine till now. 到目前为止,一切正常。 But I want this div to be hidden when the page loads and visible when the users select the image to upload. 但是我希望此div在页面加载时隐藏,而在用户选择要上传的图像时可见。 heres my code constructions: 这是我的代码构造:

 <div class="fileinput fileinput-new" data-provides="fileinput">
 <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px; display:none"></div>  #This is the div for previewing images
 <div>
 <span class="btn btn-info btn-file">
 <span class="fileinput-new">Select image</span>
 <span class="fileinput-exists">Change</span>
 <input type="file" name=""></span>

Heres my jquery: (Its my first time with jquery) 这是我的jquery :(这是我第一次使用jquery)

<script type="text/javascript">
$(document).ready(function(){
    $(".btn btn-info btn-file").click(function(){
        $(".fileinput-preview thumbnail").show();
       });
});
</script>

jsFiddle http://jsfiddle.net/3717h5mg/1/ jsFiddle http://jsfiddle.net/3717h5mg/1/

This is not working as i thought it would do. 这没有按我想的那样工作。

try 尝试

$(document).ready(function(){
    $(".btn.btn-info.btn-file").change(function(){
         $(".fileinput-preview.thumbnail").show();
    });
});

NOTE : you missed dot(.) selector in click $(".btn btn-info btn-file"), and also thumbnail class 注意:您错过了$(“。btn btn-info btn-file”)和缩略图类中的点(。)选择器

DEMO 演示

You can show the thumbnail on file change event 您可以在文件更改事件中显示缩略图

$(document).ready(function(){
 $('input[type="file"]').change(function () {
     $(".fileinput-preview.thumbnail").show();
 });
});

Bala and anspsmn said it clearly: handle 'onchange' event. Bala和anspsmn明确表示:处理“ onchange”事件。 That answer your question. 那回答你的问题。 But, maybe an Jquery plugin could seems to you a simpler option to do what you are doing. 但是,也许Jquery插件在您看来似乎是执行您正在做的事情的一个更简单的选择。 I've developed a Jquery plugin that can be easily customized for your current needs. 我已经开发了一个Jquery插件,可以轻松地针对您当前的需求对其进行自定义。 You can select images (or any other file type), preview them, crop them (if you want it), and easily upload them. 您可以选择图像(或任何其他文件类型),进行预览,裁剪(如果需要)并轻松上传。 Plus, you can set max KB for files, drag and drop files, etc., and style buttons too. 另外,您还可以为文件,拖放文件等以及样式按钮设置最大KB。

Here an example: 这里是一个例子:

$('#your-input-file').customFile({
   type : 'image',
   maxKBperFile : 500,
   maxFiles : 1
});

Here you will find download link and documentation: Jquery Custom Input File 在这里您将找到下载链接和文档: Jquery自定义输入文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM