简体   繁体   English

我想在选择的输入文件上显示图像

[英]I want to display image on select input file

My jQuery code isn't working. 我的jQuery代码无法正常工作。 I want to display an image when user selects an input file. 我想在用户选择输入文件时显示图像。 Please help. 请帮忙。

I am using data-val attribute to save the ID. 我正在使用data-val属性保存ID。 Here is my code: 这是我的代码:

</script>
    <script type="text/javascript">
        function readURL(input, id) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $(id).attr('src', e.target.result);
                }

                reader.readAsDataURL(input.files[0]);
            }
        }
        jQuery(document).on("change", "#imgInp", function(){
            var id = jQuery(this).data("val");          
            readURL(this, id);
        });

    </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" runat="server">
  <input type='file' data-val="#blash1" id="imgInp" />
  <img id="blah1" src="#" alt="your image" />
  <br>
  <input type='file' data-val="#blash2" class="imgInp" />
  <img id="blah2" src="" alt="your image" />
  <br>
  <input type='file' data-val="#blash3" class="imgInp" />
  <img id="blah3" src="" alt="your image" />                        
</form>

Try this: 尝试这个:

Html: HTML:

<form id="form1" runat="server">
  <input type='file' data-val="#blash1" class="imgInp" /> // add class imgInp here
  <img height="100" id="blah1" src="#" alt="your image" />
  <br>
  <input type='file' data-val="#blash2" class="imgInp" />
  <img height="100" id="blah2" src="" alt="your image" />
  <br>
  <input type='file' data-val="#blash3" class="imgInp" />
  <img height="100" id="blah3" src="" alt="your image" />                        
</form>

Jquery: jQuery:

function readURL(input,img_name) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#'+img_name).attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$(".imgInp").change(function(){
    var img_name = $(this).next("img").attr("id");
    readURL(this,img_name);
});

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

相关问题 我想多次将文件追加到$ _FILES输入类型文件图像选择 - I want to append the files to $_FILES on multiple times input type files image select 当我提交表单输入类型=文件以使用 php 发送图像时,我想重定向到另一个页面 - when i submit form input type=file for send image with php i want redirect to an other page 我有一个名为hastanelistesi.json的文件。在这个文件中,我想访问“hastaneAdi”并显示select-option菜单 - I have a file named hastanelistesi.json.In this file, I want to access the “hastaneAdi” and display the select-option menu 我想禁用 q-select 的动画(类星体选择输入) - I want to disable the animation of a q-select (quasar select input) 尝试显示输入文件的图像onChange事件 - Try To Display Image onChange Event of Input File 显示输入文件中的所选图像 - Display selected image from input file 显示来自表中文件输入的图像 - Display image from file input in table 我想在输入框、表格或使用 node.js 的 div 文件中显示存储在 mysql 数据库中的数据。 - I want to display data stored in mysql database in .html file on input boxes,tables,or in a div using node.js 当输入有17个字母时,我想制作显示块 - I want to make display block when there are 17 letters into input 我想禁用点(。)从输入显示 - I want to disable the dot(.) from showing on input display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM