简体   繁体   English

我无法添加第二张图片。 第一个图像输入只工作

[英]I can't add second image. first image input Only work

Why i cant access second file chooser.为什么我无法访问第二个文件选择器。 first file chooser work well.第一个文件选择器运行良好。 This is HTML part这是 HTML 部分

 <form action="<?php url("image/uploadImg"); ?>" method="post" enctype="multipart/form-data">
                <div class="imgLine">
                    <div class="line">
                        
                        <img id="previewImg" src="<?php echo BURL.'assets/img/addImg.svg'; ?>" alt="Placeholder">
                    </div>
                    <div class="line">
                        <!-- <input type="file" name="file"  onchange="previewFile(this);" required> -->
                        <label class="addFile">  Select File
                            <input type="file" name="file" size="60" onchange="previewFile(this);" required>
                        </label> 
                    </div>
                    <div class="line">
                        <input class="submitBtn" type="submit"  name="submit" value="Save">
                    </div>
                </div>

            </form>

            <form action="<?php url("image/uploadImg"); ?>" method="post" enctype="multipart/form-data">
                <div class="imgLine">
                    <div class="line">
                        
                        <img id="previewImg" src="<?php echo BURL.'assets/img/addImg.svg'; ?>" alt="Placeholder">
                    </div>
                    <div class="line">
                        <!-- <input type="file" name="file"  onchange="previewFile(this);" required> -->
                        <label class="addFile">  Select File
                            <input type="file" name="file" size="60" onchange="previewFile(this);" required>
                        </label> 
                    </div>
                    <div class="line">
                        <input class="submitBtn" type="submit"  name="submit" value="Save">
                    </div>
                </div>

This is jquery part这是 jquery 部分

<script>
function previewFile(input){
    //console.log(input);
    var file = $("input[type=file]").get(0).files[0];
    //console.log(file);
    if(file){
        var reader = new FileReader();

        reader.onload = function(){
            $("#previewImg").attr("src", reader.result);
        }

        reader.readAsDataURL(file);
    }
}

What I am getting is upon uploading the second file.我得到的是上传第二个文件。 The function call. function 电话。 parameter values also pass.参数值也通过。 but var file = $("input[type=file]").get(0).files[0] this file varible does not create.Please help但是var file = $("input[type=file]").get(0).files[0]这个文件变量没有创建。请帮忙

Just change只是改变

<img id="previewImg"

To some classes in both form对于两种形式的某些课程

<img class="previewImg"

Probably it's require changes in javascript code.可能需要更改 javascript 代码。

Change a way how you create a function. Do not run javascript function inside html code, remove it更改创建 function 的方式。不要在 html 代码中运行 javascript function,将其删除

onchange="previewFile(this);"

instead of this catch the action ie而不是这个赶上行动即

function previewFile() {
$('.previewImg').on('change', function() {
 // use $(this) to catch clicked element only
  ... your code goes here
});

}

I will find some result.我会找到一些结果。 It works well.它运作良好。 THankz who helps me谢谢帮助我的人

function previewFile(input) {

console.log(input);
var file = input.files[0];
console.log(file);

if(file){
    var reader = new FileReader();
    reader.onload = function(){
        $(input).closest('.imgLine').find('#previewImg').attr("src", reader.result);
    }
        reader.readAsDataURL(file);
}

} }

暂无
暂无

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

相关问题 V-For 图像路径找不到图像。 使用 v-for 时如何添加正确的文件路径? - V-For Image paths cannot find image. How can I add the correct file path when using v-for? 我有一个显示用户当前个人资料图像的图像元素。 我可以将该图像的 src 更改为文件输入中的图像吗? - I have an image element which displays the user's current profile image. Can I change the src of that image to the image inside the file input? Javascript幻灯片仅显示第一张图片,按钮不起作用 - Javascript slideshow only shows first image, buttons don't work Photoswipe 只打开第一张或第二张图片 - Photoswipe opens only first or second image 无法在第二张图片后使用jquery添加内容 - Can't add something after second image with jquery Chrome将图片从一个元素移动到另一个元素时会重新加载图片,Firefox和Internet Explorer只需“移动”图片即可。 我该如何解决? - Chrome reloading image when moving it from one element to another, Firefox and Internet Explorer simply *move* the image. How can I work around this? 如何仅在1秒钟内更改背景图像 - How can I change background image in 1 second for only one time 我有9个小图像,当单击它们时,它们会在图像的较大版本中消失。 为什么不能在Browser(FF)中工作) - I have 9 small images that when clicked on, will fade in the bigger version of the image. Why wont work in Browser(FF)) 我无法使用Javascript将图像添加到画布 - I can't add an Image to a canvas in Javascript 我想使用隐藏输入字段的图像上传框。 当我隐藏输入字段时,它无法正常工作..有任何建议吗? - I want to use image upload box with hiding input field . when i hide the input field it can't work properly .. any suggesition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM