【问题标题】:Show a preview image of an HTML5 <input type="file"> selected image file显示 HTML5 <input type="file"> 所选图像文件的预览图像
【发布时间】:2016-05-24 16:25:54
【问题描述】:

如果问题错了,请见谅,

我想在&lt;input type=file&gt;时在img标签中display特别是image

$(':input[type=file]').change( function(event) {
	var tmppath = URL.createObjectURL(event.target.files[0]);
	$(this).closest("img").attr('src',tmppath);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="file1" name="file1">
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img1" class="img1" height="100px" width="100px"><br>
<input type="file" id="file2" name="file2">
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img2" class="img1" height="100px" width="100px"><br>
<input type="file" id="file3" name="file3">
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img3" class="img1" height="100px" width="100px">

这是我的jsfiddle

【问题讨论】:

    标签: jquery html image


    【解决方案1】:

    试试这个:你必须使用.next()而不是.closest(),因为next用于查找下一个元素,而closest用于查找匹配的父元素。见下面代码

    $(':input[type=file]').change( function(event) {
    	var tmppath = URL.createObjectURL(event.target.files[0]);
    	$(this).next("img").attr('src',tmppath);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="file" id="file1" name="file1">
    <img src="" id="img1" class="img1" height="100px" width="100px"><br>
    <input type="file" id="file2" name="file2">
    <img src="" id="img2" class="img1" height="100px" width="100px"><br>
    <input type="file" id="file3" name="file3">
    <img src="" id="img3" class="img1" height="100px" width="100px">

    关于 .next().closest()

    的更多信息

    编辑:由于 OP 更改了 html 结构,我对答案进行了一些更改,如下所示。 通过将输入和图像元素放入 div 来制作一组输入和图像元素,并在脚本中进行必要的更改。

    $(':input[type=file]').change( function(event) {
    	var tmppath = URL.createObjectURL(event.target.files[0]);
        //get parent using closest and then find img element
    	$(this).closest("div").find("img").attr('src',tmppath);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div>
    <input type="file" id="file1" name="file1"><br>
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img1" class="img1" height="100px" width="100px"><br>
    </div>
    <div>
    <input type="file" id="file2" name="file2"><br>
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img2" class="img1" height="100px" width="100px"><br>
    </div>
    <div>
    <input type="file" id="file3" name="file3"><br>
    <font color="#FF0000">For best view upload only <b>200*250</b></font><div class="clear"></div>
    <img src="" id="img3" class="img1" height="100px" width="100px">
    </div>

    【讨论】:

    • 很高兴为您提供帮助:)
    • 你好@Bhushan你能再帮我一次吗
    • 我已经对我的代码进行了一些更改,现在我想要与它相关的解决方案
    • 请将其作为新问题发布并尽可能提供 jsfiddle
    • 您可以通过将输入和图像包装到div 中来对它们进行分组,这样div 元素将成为父元素。然后使用最接近获取父级并使用find 获取图像并更改其src 值。见this。因此,当您将来添加删除任何元素时,它不会影响您的代码:)
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-04-19
    • 2010-11-27
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多